14

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.

4
  • i think you should addtobackStack() first and then replace the fragment... Commented Feb 11, 2013 at 4:15
  • I did try that, and I added the following method into my Activity - which didn't work. pastebin.com/aezQD48p Commented Feb 11, 2013 at 7:58
  • Could the problem be the fact that I use the SupportManager instead of Manager? I can see the question you linked has to do with ChildFragmentManager - mine is SupportManager. Commented Feb 11, 2013 at 8:00
  • Actually addToBackStack(null) wont work with Fragment and AppCompatActivity. However it works with v4 Fragment and AppCompatActivity (verified) Commented Feb 24, 2016 at 9:57

3 Answers 3

54

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();
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

This works but it does not replay the animation. EDIT: If you want to play the animation on the popbackstack, add two extra parameters when you call setCustomAnimation. See here.
This works nicely. It's surprising that we have to handle it ourselves because I guess the documentation says that this will happen automatically.
if this is documented somewhre?
Used to work without it, but I recently had the same problem and this fixed the issue...
You don't need to do this, if your activity extends from FragmentActivity and transactions are added using getSupportFragmentManager. getSupportFragmentManager().beginTransaction().addToBackStack()
|
12

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.

2 Comments

Also, I don't have a custom constructor for my Fragment - I didn't change it. I do though have a method in the Fragment that serves as a view updater.
Okay, I posted the log link from the Manager.
2

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();

2 Comments

addToBackStack when performing the add/replace of the fragment is what did it for me.
"call addToBackStack(null) before add(), not after" This is exactly what was my issue. You saved me a ton of headache. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.