5

I call this method to go forward from AFrag to BFrag:

showFragment()
{ 
    FragmentTransaction fragmentTransaction = mFragmentMgr.beginTransaction();

    // Add fragment to the container ContentView 
    fragmentTransaction.replace(R.id.operation_fragments_frame, mBFrag, mBFrag.getTag());

    // Add FADE effect
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);   

    // Keep the transaction in the back stack so it will be reversed when backbutton is pressed
    fragmentTransaction.addToBackStack(null);

    // Commit transaction
    fragmentTransaction.commit();
}

It shows a new fragment (BFrag), replace the previous one (AFrag) and keep info about the transaction, so it can be reversed/undone automatically on back button pressed.

When back button is pressed everything looks fine, the previous fragment is shown (AFrag). But when I go forward again (AFrag -> BFrag) I got a "Fragment already added exception".

Didn't the reverse/undone operation remove the new fragment (BFrag)? Is this the expected behaviour?

That's weird because after this, I decided to set a check:

 if(mBFrag.isAdded()) 
 {
    fragmentTransaction.show(mBFrag);
 }
 else 
 {
   fragmentTransaction.replace(R.id.operation_fragments_frame, mBFrag, mBFrag.getTag());
 }

and stills, it gets into the else statement... and I get the exception.

Any insight on what am I doing wrong, please?

Thx.

Axel M. Garcia
  • 5,138
  • 9
  • 27
  • 29

1 Answers1

4

have you tried to use an other method, like remove(), then do an add(). or anything similar? I saw on some other post that the replace() method does not always behave correctly.

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
Christ
  • 450
  • 6
  • 10
  • Yep, the answer and the other post you are talking about are mine buddy xD http://stackoverflow.com/questions/6250580/fragment-already-added-illegalstateexception Thx anyway – Axel M. Garcia Jun 10 '11 at 08:13
  • I am glad to hear this, I was just wondering the error was with my code, as always. Thank you for clarifying. – Eduardo Reis Aug 02 '17 at 17:02