Multiple Backstack Navigation (Navigation Component)
As of now Navigation Component doesn't support multiple backstack management out of the box most commonly used in Bottom Navigation.
Google already has an Advanced Navigation Sample which showcases handling of multiple backstacks.
- It always takes the user back to the first tab irrespective for the order they were opened.
- Subjective approach, it attaches and detaches the navigation graph each time user navigates through bottom navigation view.
- It's difficult to add custom backpress action (ex: saving the data on certain screen before switching tabs) through their implementation.
There are other resources as well to solve these issues, most commonly using ViewPager: Instagram style navigation using Navigation Component
Major Issue: App crashes on configuration change i.e "no nav controller attached on configuration change"
This repository/sample solves all the issues mentioned above.
It takes consideration of user actions and navigates use back in the same order.
User action: Tab 1 -> Tab 2 -> Tab 3 -> Tab 2
Result: Tab 1 -> Tab 3 -> Tab 2
In my approach, I am following show/hide of navigation graph, once they are created. While using
Fragment Manager's show/hide, it doesn't call
onResume(). So, I achieved that by setting their max lifecycle using
I can set custom backpress action before switching tab's navigation.
Brownie Point: It supports configuration change and restores the from the same state the navigation was on.