Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fragment not added to Back Stack

I am doing a NavigationDrawer based application. I have an hierarchy like given below

NavigationDrawer --> RootFragment(Not added to back Stack) --> Detail Fragment (Added to Back Stack)

Now, I am trying to show a message to user when he tries to exit the app by pressing back button. Here is the code I am using for it.

@Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() > 0) {
            getFragmentManager().popBackStack();
        }
        else if (getFragmentManager().getBackStackEntryCount() == 0) {
            new AlertDialog.Builder(this)
                    .setMessage("Are you sure you want to exit?")
                    .setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            MainActivity.this.finish();
                        }
                    })
                    .setNegativeButton("No", null)
                    .show();

        }
        else
        {
            super.onBackPressed();
        }
    }

When I click back button from the detail fragment, which is added to back stack, I am getting the alert message. Instead I would except to go back to the root fragment.

But, If I update the code like this, pressing the back button takes the user back to the root view. So it looks like getFragmentManager().getBackStackEntryCount() is Zero even if the detailed fragment is added to back stack.

@Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() > 0) {
            getFragmentManager().popBackStack();
        }
        else
        {
            super.onBackPressed();
        }
    }

Here is how I call the detail fragment from the rootFragment.

FragmentManager fragmentManager = getFragmentManager();
                Fragment fragment = SubCategoryListFragment.newInstance(false);
                fragmentManager.beginTransaction()
                        .add(R.id.subCategoryDetails, fragment)
                        .addToBackStack(null)
                        .commit();

Here is the root view which is opened form the navigation drawer side menu.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/categoriesRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"/>

    <FrameLayout
        android:id="@+id/subCategoryDetails"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>


</FrameLayout>

What am I doing wrong here? Whats the correct way to implement this?

Thanks.

like image 626
Zach Avatar asked Jan 24 '16 14:01

Zach


People also ask

How can I maintain fragment state when added to the back stack?

Solution: Save required information as an instance variable in calling activity. Then pass that instance variable into your fragment.

What is add to Backstack?

Calling addToBackStack() commits the transaction to the back stack. The user can later reverse the transaction and bring back the previous fragment by pressing the Back button. If you added or removed multiple fragments within a single transaction, all of those operations are undone when the back stack is popped.

What does addToBackStack null do?

Use . addToBackStack(null) : If you don't want later pop more than one back stack, but still want to pop one at a time.


2 Answers

Assuming you are using support fragments, you have to use getSupportFragmentManager() in your Activity. With getFragmentManager() you are getting the native fragment manager. However, in support fragment classes, getFragmentManager() returns the support fragment manager, which may be a bit confusing.

like image 67
patloew Avatar answered Oct 07 '22 18:10

patloew


Pass a valid tag instead of null in this statement

addToBackStack(null) 
like image 2
nvinayshetty Avatar answered Oct 07 '22 18:10

nvinayshetty