From my investigations today I believe that FragmentManager
does actually survive activity destruction due to configuration changes. This is because if you add a fragment with a tag to the fragment manager (for example, fragmentManager.beginTransaction().replace(R.id.container, myFragment, "blah").commit()
), then you can still retrieve that fragment with fragmentManager.findFragmentByTag("blah")
even after the activity has been destroyed and re-created due to a configuration change. You can see from the source code that it retains a ArrayList<Fragment> mAdded
which contains all of these previously added fragments. If the FragmentManager
was destroyed and re-created, this mAdded
would have become an empty list, which clearly isn't the case as above.
HOWEVER, the nature of what is retained within each fragment depends on setRetainInstance
. If you don't set retain, then only the fragment arguments and saved instance state are persisted, and the fragment instance is re-created by the framework. If you do set retain, then the whole instance (including fields) is persisted. However either way, the FragmentManager
itself is still persisted otherwise we wouldn't be able to retrieve tags from it anymore.
While the persistence of FragmentManager
across configuration changes is not explicitly mentioned by the documentation, I believe it's implied by statements such as the following:
during a configuration change, your activity and all of its fragments are destroyed and then recreated with the most applicable Android resources. The FragmentManager handles all of this for you. It recreates instances of your fragments, attaches them to the host, and recreates the back stack state.
i.e. if the FragmentManager
was not persisted across the configuration change, it could not continue to manage this after activity re-creation.