Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - addToBackStack() doesn't work?

This is the function responsible for adding fragments to back stack:

public void populateContent(File f)
{

    ContentFragment cf = new ContentFragment(ctx, ac, this);
    FragmentTransaction transaction = ac.getSupportFragmentManager().beginTransaction();;
    cf.updateView(f);

    transaction.replace(R.id.contentFragment, cf);

    transaction.addToBackStack(null);

    transaction.commit();

}

When I click the back button, the last fragment doesn't get loaded (nothing happens).

Any idea what might be causing this?

Edit: FragmentManager log.

http://pastebin.com/mYnVdkLG

It seems to me as if my application is saving the second view twice, instead of saving the first one and then the second view.

like image 374
Tool Avatar asked Feb 11 '13 03:02

Tool


3 Answers

It seems that calling addToBackStack() on fragment transaction is not enough, we have to handle the popping up of the back stack upon Back button pressed by ourselves. I added this to my activity and it worked as expected:

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0 ){
        getFragmentManager().popBackStack();
    } else {
        super.onBackPressed();
    }
}
like image 55
Tony Vu Avatar answered Nov 15 '22 21:11

Tony Vu


I am not sure about the actual solution but I can guide you a bit and perhaps you can figure out what the problem is.

Are you actually replacing two fragments? If you do not then there is no transaction to revert. Also is your first Fragment added from XML? The manager will not know about this fragment and you might need to add the first Fragment using a transaction too.

Be careful to check for if (savedInstanceState == null) performFirstTransaction() otherwise you will end up adding your first Fragment twice.

One good idea is to use enableDebugLogging in the FragmentManager. This will tell you about which fragments the manager knows about.

See this: http://developer.android.com/reference/android/app/FragmentManager.html#enableDebugLogging(boolean)

As a side note, it is NOT recommended to use a custom constructor for your Fragment. That is because if your app gets killed and re-instantiated by the OS it will call the empty constructor.

You should use a static method such as ContentFragment.getInstance(<params>) to create your Fragment.

See more info at: http://developer.android.com/reference/android/app/Fragment.html at the "Class Overview" section.

I hope my answer helps you a little bit to find the problem.

like image 26
dnkoutso Avatar answered Nov 15 '22 20:11

dnkoutso


I was adding, not replacing, but I had to call addToBackStack(null) before add(), not after, on the fragment which will close, not on the fragment which will stay open.

In class A (opened first)

FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.layout_a, f, Constants.FRAGMENT_KEY);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();

In class B (opened by class A)

FragmentTransaction ft = fm.beginTransaction();
ft.addToBackStack(null);
ft.add(R.id.layout_b, f, Constants.FRAGMENT_KEY);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
like image 43
Susanne Siverland Avatar answered Nov 15 '22 21:11

Susanne Siverland