ActionBarActivity back button not popping from backstack ActionBarActivity back button not popping from backstack android android

ActionBarActivity back button not popping from backstack


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 current Activity.

In fact, according to the onBackPressed() official documentation:

Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.

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 :

@Overridepublic void onBackPressed(){    if (mDrawerLayout.isDrawerOpen()) {        mDrawerLayout.closeDrawer();    } else if (getFragmentManager().getBackStackEntryCount() > 0) {        getFragmentManager().popBackStack();    } else {        super.onBackPressed();    }}

This way, ActionBarActivity.onBackPressed() is only called when the backstack is empty, in which case it destroys the ActionBarActivity.


You should check "getFragmentManager" & "getSupportFragmentManager" is matched your activity & actionbaractivity or not.

Because, in Activity:

public void onBackPressed() {    if (!mFragments.popBackStackImmediate()) {        finish();    }}

in FragmentActivity:

public void onBackPressed() {    if (!mFragments.popBackStackImmediate()) {        finish();    }}

We can see the same code which already handled pop fragments backstatck.In my situation, I used actionbaractivity(extends FragmentAcvitiy), but I also used "getFragmentManager" , so I got the same error as you. After I have replaced "getFragmentManager" to "getSupportFragmentManager", that's ok! You also can replace "actionbaractiviy" to "Activity" to fix this problem.

Must ensure "getFragmentManager" match "Activity", "getSupportFragmentManager" match "FragmentActivity(ActionbarActivity)".

If you want add actionbar On API level 11 or higher, You can see below:

https://developer.android.com/guide/topics/ui/actionbar.html#Adding

On API level 11 or higher The action bar is included in all activities that use the Theme.Holo theme (or one of its descendants), which is the default theme when either the targetSdkVersion or minSdkVersion attribute is set to "11" or higher. If you don't want the action bar for an activity, set the activity theme to Theme.Holo.NoActionBar.