architecture-components-samples
architecture-components-samples copied to clipboard
NavigationAdvancedSample - navigating between stacks.
Are there any examples on how to navigate between stacks?
For example, if I want to navigate from the Register screen directly to the Home/About screen how do I do this? Additionally how would I maintain the backstack so that when I hit the back button while on the About screen I would go back to the Register screen instead of the Title Screen.
To put the problem more generically: If I have bottom nav sections A, B and C, each with fragments, 1, 2 and 3 - How do I nav from Section A Fragment 1 to Section B Fragment 2?
You solved this? also need this
No. Our workaround is to have 2 instances of the same fragment, one in each section.
really need this
Also have similar problem.
I have created a sample that does something like that. You can check it out here https://github.com/St4B/Jalapeno
In our app we did add a custom Navigator that deals with Navigation between tabs.
It is added to each fragment container when setupWithNavgraph is called like this: navHostFragment.navController.navigatorProvider += bottomNavigationNavigator
bottomNavigationView is passed into it as a constructor parameter selectedNavController is the live data of the selected navController
We added the ability to also navigate to a destination within that graph, after working with the navigation library for longer now, you probably can improve this code. By checking each graph if they support the destination you want to go to. Similar to the setupDeepLink method. Essentially it does this:
const val BOTTOM_NAVIGATION_VIEW_NAVIGATOR = "bottomNavigation"
@Navigator.Name(BOTTOM_NAVIGATION_VIEW_NAVIGATOR)
class BottomNavigationViewNavigator(
private val bottomNavigationView: BottomNavigationView,
private val selectedNavController: LiveData<NavController>
) : Navigator<BottomViewDestination>() {
override fun navigate(
destination: BottomViewDestination,
args: Bundle?,
navOptions: NavOptions?,
navigatorExtras: Extras?
): NavDestination? {
if (destination.tabId != bottomNavigationView.selectedItemId) {
bottomNavigationView.selectedItemId = destination.tabId
if (destination.id == destination.tabId) {
return destination
}
}
destination.navDestinationId?.let { navDestinationId ->
selectedNavController.value?.navigate(
navDestinationId,
args,
navOptions,
navigatorExtras
)
}
return destination
}
override fun createDestination(): BottomViewDestination {
return BottomViewDestination(R.id.action_tab_0, null)
}
override fun popBackStack(): Boolean {
return false
}
}
@Parcelize
class BottomViewDestination(var tabId: Int, val navDestinationId: Int?) :
NavDestination("bottomTabViewDestination"), Parcelable
Does anyone get a solution now?
I have created a sample that does something like that. You can check it out here https://github.com/St4B/Jalapeno It seems not what we want. We want the bottom navigate view switch from A tab to B tab, and the B tab's back stack navigate to specified fragment.