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.
Solution: Save required information as an instance variable in calling activity. Then pass that instance variable into your fragment.
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.
Use . addToBackStack(null) : If you don't want later pop more than one back stack, but still want to pop one at a time.
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.
Pass a valid tag instead of null in this statement
addToBackStack(null)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With