Retaining state with headless fragments
2014-07-29Some articles:
- Official Fragments guide. Has some clues for headless fragments (see “Adding a fragment without a UI”).
- Superb multi-pane fragments tutorial from Lars Vogel. Contains some tips for headless fragments.
- Android best tip to work with fragments and orientation change. More detailed headless fragments technique description.
- Headless fragment example - FragmentRetainInstance.java. In this code headless fragment is created from other fragment. In my case it’s inappropriate because of Robolectric testing (recursive fragment transactions).
- Saving (And Retrieving) Android Instance State – Part 2. Details about retained fragments. Neat retained fragment lifecycle.
- Handling Configuration Changes with Fragments. Main advice - Manage the Object Inside a Retained Fragment.
- Android Activity/Fragment life cycle analysis. Full logging of all lifecycle method calls in different situations.
And StackOverflow discussions:
- Unable to use Fragment.setRetainInstance() as a replacement for Activity.onRetainNonConfigurationInstance(). How to preserve fragments with WebViews and AsyncTasks. One of the answers is to override Fragment.onDestroy() and save asynctasks to Application object.
- Further understanding setRetainInstance(true). Retained fragment lifecycle logs.
- Android Fragments. Retaining an AsyncTask during screen rotation or configuration change. Retained fragment with inner asynctask example and much more. AsyncTask has reference to fragment - it’s interesting.