Android Fragment not added to Back Stack
Asked Answered
K

2

7

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.

Kiefer answered 24/1, 2016 at 14:58 Comment(0)
A
3

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.

Alwin answered 24/1, 2016 at 15:5 Comment(0)
S
2

Pass a valid tag instead of null in this statement

addToBackStack(null) 
Sudd answered 24/1, 2016 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.