Fragment BackStack Navigation: A short documentation of some important flows and how to achieve them.
There is almost no serious android application that does not use fragments except the serious thing about the app is that it does not use fragments (Ha! rhymes đ).
Okay seriously, I have had to work with fragments more times than I can remember and had to endure the pain of getting the right navigation flow for the apps I worked on. This is a short documentation of the flows I have learnt along the way. I hope to learn more and I hope someone learns from this. If you have some not included here, PLEASE đ drop it in the comments.
Navigation Flows
Take user to a previous fragment:
This is handled by the android framework as it is the most common scenario. It is what happens when user taps the back button. You can do it yourself like this:
Navigate to a known fragment In the backstack:
Youâll find this handy if for example,
- You have a parent/general and child/detail screen.
- There are ways to open the child screen without going through the parent screen.
- You want the child screens to always lead back to parent screen when user presses back or performs some action.
Since you know the parent screen name, just pop all screens before the parent screen and if the parent screen does not yet exist in the backstack, create a new instance of it.
Note that for popBackStackImmediate(tag, flag) to work as used above, it requires as a first argument, the same tag used to save the fragment to backstack (supplied to addToBackStack() ) call. I think it is good practice to save your fragment transactions with the name of the fragment; this is the convention I use. As a second argument, 0 is passed in as the flag. By passing in 0 you specify that all fragments before the fragment saved with the specified tag (letâs call this fragment FragA) should be popped. This means that FragA will now be at the top of the backstack.
Create an artificial backstack:
I discovered this one watching this Google I/O talk. Say, for example you have a payment application, after successful payment, you should not allow users to go back to the payment flow/screen when they tap the back button. This is where this method come in handy. Also, for screens launched from notifications, you could use this to determine what the user sees when he/she navigates backwards. Developers can easily create a backstack history for a screen they want shown so that when users navigate backwards, that history will be played out. In the first example, on the congratulatory page(after the successful payment), a new backstack could be created so when users navigate back, they will only see what is in the backstack. In the code above, navigating back from Fragment2 will show Fragment1 and HomeFragment, in that order
Take user back x times:
I discovered this method because I had to deal with a weird implementation. I cannot remember the exact scenario I used it for. Regardless, if you need to do this, this is how to:
Take User back to a fragment before a known fragment:
Consider an e-book app, say there is more than one way to implicitly launch the registration flow:
- When an unregistered user reads more than the 2 chapters in a story.
- When an unregistered user wants to buy an in-app product.
In either case, you want the user to continue from where he/she was before the registration screen was launched. To achieve this, you could pop off the registration screen fragment, together with all other fragments committed after it. This is how to achieve that:
In case you didnât notice, the main difference between this flow and the second flow (Navigate to a known fragment In the backstack) is the flag. In this case, the flag used is FragmentManager.POP_BACK_STACK_INCLUSIVE. This flag instructs that including the fragment saved to backstack with the specified tag, all other fragments added after it should be popped off. In our example, this returns the user to the state he/she was before registration flow started.
These are all I could think of and probably, all I know. If you know of any not included here, please drop it in the comments; I would really appreciate that. If you have questions, concerns, corrections etc please feel free to let me know; comments or otherwise.
Donât forget to clap đ if you enjoyed this. Yes, you can go up to 50 claps. You can also follow me on Twitter