I'm interested in the best way to have a single activity that switches between two fragments.
I've read probably 15 Stack Overflow posts and 5 blogs posts on how to do this, and, while I think I cobbled together a solution, I'm not convinced it's the best one. So, I want to hear people's opinions on the right way to handle this, especially with regards to the lifecycle of the parent activity and the fragments.
Here is the situation in detail:
- A parent activity that can display one of two possible fragments.
- The two fragments have state that I would like to persist across a session, but does not necessarily need to be persisted between sessions.
- A number of other activities, such that the parent activity and the fragments could get buried in the back stack and destroyed due to low memory.
- I want the ability to use the back button to move between the fragments (So as I understand it, I can't use setRetainInstance).
In addition to general architecture advice, I have the following outstanding questions:
- If the parent activity is destroyed due to low memory, how do I guarantee that the states of both fragments will be retained, as per this post: When a Fragment is replaced and put in the back stack (or removed) does it stay in memory?. Do I just need a pointer to each fragment in the parent activity?
- What is the best way for the parent activity to keep track of which fragment it is currently displaying?
Thanks in advance!