architecture-components-samples icon indicating copy to clipboard operation
architecture-components-samples copied to clipboard

NavigationAdvancedSample - navigating between stacks.

Open smithaaron opened this issue 6 years ago • 8 comments

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?

smithaaron avatar May 09 '19 11:05 smithaaron

You solved this? also need this

deckyfx avatar May 21 '19 10:05 deckyfx

No. Our workaround is to have 2 instances of the same fragment, one in each section.

smithaaron avatar May 21 '19 12:05 smithaaron

really need this

AkashiWen avatar Nov 13 '19 06:11 AkashiWen

Also have similar problem.

TostF avatar Feb 04 '20 11:02 TostF

I have created a sample that does something like that. You can check it out here https://github.com/St4B/Jalapeno

St4B avatar Feb 16 '20 10:02 St4B

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

twobeeronebread avatar Mar 03 '20 00:03 twobeeronebread

Does anyone get a solution now?

yangjunfei-lotus avatar Aug 10 '20 08:08 yangjunfei-lotus

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.

yangjunfei-lotus avatar Aug 10 '20 08:08 yangjunfei-lotus