Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBarActivity back button not popping from backstack

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?

like image 986
Lachlan Goodhew-Cook Avatar asked Nov 20 '14 01:11

Lachlan Goodhew-Cook


2 Answers

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 :

@Override
public 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.

like image 75
MathieuMaree Avatar answered Oct 18 '22 19:10

MathieuMaree


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.

like image 45
kmfish Avatar answered Oct 18 '22 19:10

kmfish