17

Any thoughts on the following code? In my testing I've found the replaced fragment isn't destroyed and the instance is still around when popping the back stack. Just looking to verify that this is a valid way to use fragment transactions.

getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(frame, fragmentB).commit();

My reason for using replace is that it causes the replaced fragment to run it's exit animation.

bgolson
  • 3,460
  • 5
  • 24
  • 41
  • I'm guessing you purposely want to keep the replaced fragment but don't want to pop it back? – Jason Hu Nov 07 '13 at 20:43
  • It will be popped back, which is why i'm adding it to the back stack. My reason for using replace is that it causes the replaced fragment to run it's exit animation. – bgolson Nov 07 '13 at 21:05
  • 2
    In that case you are using it as you should. Refer to http://developer.android.com/guide/components/fragments.html and search for `transaction.addToBackStack(null);`. It's the same. – Jason Hu Nov 07 '13 at 21:08
  • Oh wow! Didn't expect to see that in the docs :) Submit that as the answer and I'll accept it. Thanks Jason! – bgolson Nov 07 '13 at 22:22
  • 1
    I think you forget to commit. getFragmentManager().beginTransaction().replace(R.id.container, new ArticleFragment()).addToBackStack(null).commit(); – Zar E Ahmer Jun 27 '14 at 10:35
  • thanks for pointing out the missing commit, it was there in my code, just forgot to type it here. – bgolson Jun 27 '14 at 15:02

1 Answers1

24

You can refer to the android designer guide for fragment transaction: http://developer.android.com/guide/components/fragments.html

Specificly the snippet below:

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

So yes, what you are doing is the correct approach in replacing fragments.

Jason Hu
  • 1,237
  • 2
  • 15
  • 29
  • will newFragment in this case always be created or existing instance of it will be use? For example: We have Fragment_1, Fragment_2, Fragment_3. We call transaction.replace(container, Fragment_X) several times in this order: 1, 2, 3, 1, 3, 2, 1 etc.. Will all occurrences of Fragment_1 be THE same or all of them will be recreated whenever we use them in transaction.replace(...)? 10q – Ewoks Mar 29 '14 at 11:31
  • 1
    @Ewoks `transaction.replace` is only replacing what's currently in the fragment container to whatever fragment you pass in to the 2nd parameter. So in the answer, I'm creating a new `ExampleFragment` each time, thus the fragment is brand new each time. You don't necessarily have to recreate the fragments if you have the reference to it, just call `transaction.replace` with a reference to the fragment you held on to. But in your example, if you choose to hold onto the same Fragment_1/2/3, the backstack is gonna be weird for the user navigating backwards. – Jason Hu Apr 04 '14 at 18:07