Rotation and Retained Fragments

Let’s take a closer look at how retained fragments work. Retained fragments take advantage of the fact that a fragment’s view can be destroyed and recreated without having to destroy the fragment itself.

During a configuration change, the FragmentManager first destroys the views of the fragments in its list. Fragment views always get destroyed and recreated on a configuration change for the same reasons that activity views are destroyed and recreated: If you have a new configuration, then you might need new resources. Just in case better matching resources are now available, you rebuild the view from scratch.

Next, the FragmentManager checks the retainInstance property of each fragment. If it is false, which ...

Get Android Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.