I'd like to achieve navigation like this (picture below), where fragments A,B,C,D represent navigation down the application's information hierarchy.
On the phone only one fragment will be visible, and on the tablets there are two fragments visible.
All of the tutorials and documents I've seen (for example this and this) mention Master-Detail view, where it seems there is a master fragment on the left and a detail view on the right, but nobody seems to specify how to drill deeper from the detail.
In fact, GMail app for tablets works as my own app. Let's say that fragment A is list of accounts and folders, fragment B is list of e-mails and fragment C is the conversation itself.
This screen is configuration with fragments A and B
This screen has fragments B and C
The question is: Should I implement this with a single Activity, where
- for phones I will have one FrameLayout for the fragments
- for tablets I will have two FrameLayouts for the 'left' and 'right' fragment?
If so, how will I insert the new fragments? In FragmentTransaction I have two actions available - add()
and replace()
.
- If I use
add()
, then the fragments below seem to be alive (they don't enter paused state) and retain their complete state (but presumably waste some resources) (so in Fragment D, all of C, B and A will still be there), and on the back button the scroll state and loaded data will still be there - If I use
replace()
, then the previous fragment will be removed, so there will be only one (on the phone) fragment on the top in any moment, and on the back button the transaction will get reversed, old fragment added back, but it won't remember its previous state
None of these options seems right to me. The tutorials use replace() in the FragmentTransaction - but how do they restore the state of the previous fragment, for example in the GMail app for phone the fragment B is loaded from the server with endless scrolling, then when I replace it with fragment C and go back, B should appear back where it was left off, right?
I really don't know what to do or where to look, please advise.