I am designing an app on the Android with a Navigation Drawer. Lets say I navigate through fragments and go from Fragment 1
to Fragment 2
. Everything works fine but when I am in Fragment 2
(which loads from the navigation drawer) and click the system back button althought I get the previous fragment (I use addToBackStack
) the navigation drawer doesn't get updated and the cell of Fragment 2
is highlighted. What should I do to fix this?
Asked
Active
Viewed 3,270 times
4

alecnash
- 1,750
- 1
- 18
- 42
-
Is that even how it is supposed to work? I just tried Google Play Books and Play Movies - both have been updated to use the new Nav Drawer - and fragments opened from the drawer are not added to the back stack. – Tom Jun 29 '13 at 00:18
-
I don't know! I am also not sure but if you see the Gmail app it always returns to the inbox. So I suppose it's just a matter of taste – alecnash Jun 29 '13 at 14:59
-
Just checked the Android docs for the NavDrawer, and there is no addToBackStack in their getItem, reinforcing my impression that back is not supposed to take you to the previous nav drawer page. On the other hand, I guess Google's inconsistency suggests that it is still up for debate and personally I think the way you are doing it makes more sense. – Tom Jun 29 '13 at 18:28
2 Answers
3
Found the solution:
Added a tag in every addToBackStack
. So the code if I call addToBackStack
it looks like this:
addToBackStack("Fragment1");
addToBackStack("Fragment2");
whenever I put each fragment to the stack. Then I override the back button pressed:
@Override
public void onBackPressed() {
super.onBackPressed();
FragmentManager fm = getSupportFragmentManager();
String stackName = null;
for(int entry = 0; entry < fm.getBackStackEntryCount(); entry++){
stackName = fm.getBackStackEntryAt(entry).getName();
Log.i("BC", "stackEntry" + entry);
}
if (stackName == "Fragment1"){
mDrawerList.setItemChecked(0, true);
} else if (stackName == "Fragment2") {
mDrawerList.setItemChecked(1, true);
}
}

alecnash
- 1,750
- 1
- 18
- 42
-
7String equality should be evaluated with .equals() rather than == – Brian Melton-Grace - MSFT Jul 17 '14 at 14:00
0
The question is 2 years old and the answer of @alecnash is working. But in my opinion he is misappropriating the onBackPressed()
method... and for later googler:
Better use an OnBackStackChangedListener
. In this approach you need not to override the onBackPressed()
which you probably need for somethingelse. Together with the code from @alecnash the listener looks like:
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
FragmentManager fm = getSupportFragmentManager();
String stackName = null;
for(int entry = 0; entry < fm.getBackStackEntryCount(); entry++){
stackName = fm.getBackStackEntryAt(entry).getName();
Log.i("BC", "stackEntry" + entry);
}
if (stackName == "Fragment1"){
mDrawerList.setItemChecked(0, true);
} else if (stackName == "Fragment2") {
mDrawerList.setItemChecked(1, true);
}
});

Bolic
- 705
- 2
- 12
- 30