Caveat when adding fragments via XML


I spent an hour trying to figure our why, after replacing or removing a Fragment, the UI of the fragment persisted (even though subsequent attempts to remove it would fail because it was supposedly already gone).

The answer was finally found at the bottom of the Creating a Fragment lesson.

“Note: When you add a fragment to an activity layout by defining the fragment in the layout XML file, you cannot remove the fragment at runtime. If you plan to swap your fragments in and out during user interaction, you must add the fragment to the activity when the activity first starts, as shown in the next lesson.”

I was in fact creating the fragment by adding it to my XML layout. It’s a shame that they don’t mention this in the main Fragments Guide.