Sunday, July 13, 2014

Android Fragment Replace vs. Remove+Add and the Backstack

The Android documentation has this to say about FragmentTransaction.replace:

Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.

I found the documentation to be slightly misleading because there is an important difference between replace vs. remove+add when the backstack is involved:

  1. If the backstack A->B->C is built using remove+add, then popped back to fragment A, then fragment B's onResume method will be triggered.
  2. If the backstack A->B->C is build using replace, then popped back to fragment A, then fragment B's onResume method will NOT be triggered.

No comments:

Post a Comment