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.

Tool
  • 12,126
  • 15
  • 70
  • 120
  • i think you should addtobackStack() first and then replace the fragment... – user1969053 Feb 11 '13 at 04:15
  • I did try that, and I added the following method into my Activity - which didn't work. http://pastebin.com/aezQD48p – Tool Feb 11 '13 at 07: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. – Tool Feb 11 '13 at 08:00
  • Actually addToBackStack(null) wont work with Fragment and AppCompatActivity. However it works with v4 Fragment and AppCompatActivity (verified) – deadfish Feb 24 '16 at 09:57

3 Answers3

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();
    }
}
Tony Vu
  • 4,251
  • 3
  • 31
  • 38
  • 4
    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](http://stackoverflow.com/questions/25485822/android-pop-fragment-from-backstack-with-animation). – Nathan Jun 13 '15 at 19:07
  • 2
    This works nicely. It's surprising that we have to handle it ourselves because I guess the documentation says that this will happen automatically. – Ankit Sep 15 '15 at 09:38
  • if this is documented somewhre? – Defuera Oct 12 '15 at 14:41
  • Used to work without it, but I recently had the same problem and this fixed the issue... – Warpzit Nov 14 '15 at 14:57
  • You don't need to do this, if your activity extends from `FragmentActivity` and transactions are added using getSupportFragmentManager. `getSupportFragmentManager().beginTransaction().addToBackStack()` – vdua Feb 06 '16 at 18:06
  • you just saved me alot of headache! – oxyt Mar 29 '16 at 11:28
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.

dnkoutso
  • 6,041
  • 4
  • 37
  • 58
  • 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. – Tool Feb 11 '13 at 07:47
  • Okay, I posted the log link from the Manager. – Tool Feb 11 '13 at 07:49
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();
  • addToBackStack when performing the add/replace of the fragment is what did it for me. – Clocker Nov 24 '15 at 03:10
  • *"call addToBackStack(null) before add(), not after"* This is exactly what was my issue. You saved me a ton of headache. Thanks! – Gautham C. Mar 28 '17 at 23:27