I'm using this template https://github.com/kanytu/android-material-drawer-template just to try out material design
so I've implemented a few fragments some have webviews some have not.
My problem is when switching between the fragments I can see them being successfully added to the backstack
getFragmentManager().beginTransaction().replace(R.id.container, new FAQ()).addToBackStack("FAQ").commit();
But when I press the back button it just closes the app.
When I change it to use Activity
instead of ActionBarActivity
the navigation works fine but I lose some other functionality.
There is an override on the back button
@Override
public void onBackPressed() {
if (mNavigationDrawerFragment.isDrawerOpen())
mNavigationDrawerFragment.closeDrawer();
else
super.onBackPressed();
}
but even if that's removed it still happens. I think the problem lies somewhere in the super.onBackPressed
Is there any reason ActionBarActivity
would break the back button?
I recently read a post about this, sorry I can't find it anymore... But basically, it explained that the primary function of the back button is to finish the currentActivity
.In fact, according to the
onBackPressed()
official documentation:And it would appear that even though the back button used to pop the backstack before 5.0, Google would have changed this behaviour with the new
ActionBarActivity
.For my part, I used some workarround that works for me but that might not work for everybody, depending on your navigation implementation.
But in case it could be helpful to somebody, here it is :
This way,
ActionBarActivity.onBackPressed()
is only called when the backstack is empty, in which case it destroys theActionBarActivity
.