Fragment Behavior: FragmentTransaction :: replace () and backStack inverse operation

I call this method to go 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), replaces the previous one (AFrag) and saves transaction information, so it can be automatically undo / undo on the back button.

When you click the "Back" button, everything looks fine, the previous fragment (AFrag) is displayed. But when I go ahead again (AFrag → BFrag), I got an exception "Fragment already added."

Did the reverse / undone operation delete the new fragment (BFrag)? Is this expected behavior?

This is strange because after that 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 an exception.

Any understanding of what I'm doing wrong, please?

thank.

+5
1

, remove(), (). - ? - , replace() .

+4