Full Guide to Nested Navigation Graphs in Jetpack Compose

Philipp Lackner · 85K views · 2023 · 12:01

Open on YouTube

Software (other)Software architecture & designtutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about nested nav graphs and sharing a ViewModel between Compose navigation destinations.

Recommended by 3 people across 3 different months, 2023 to 2024.

What it is

A 12-minute Android tutorial by Philipp Lackner on setting up nested navigation graphs in Jetpack Compose. It covers grouping destinations under parent routes and sharing a ViewModel across screens in the same graph, with source code on GitHub.

What people said

Recommends heet kanabar · 2023-06 · Stack Overflow · on “Android ViewModel - Nested UiState”

@MaelFosso then I would suggest you to use nested navigation and here I have a video for full explanation :- 1) [this video] 2) [LINK] this might help you.

Recommends Geraldo Neto · 2023-08 · Stack Overflow · on “Sharing viewModel within Jetpack Compose Navigation”

A recommended approach [LINK] , if you want to access the ViewModel scoped to navigation routes or the navigation graph i.e. that is shared between navigation routes or the navigation graph, you should use: @Composable fun MyApp() { val navController = rememberNavController() val startRoute = "example" val innerStartRoute = "exampleWithRoute" NavHost(navController, startDestination = startRoute) { navigation(startDestination = innerStartRoute, route = "Parent") { composable("exampleWithRoute") { backStackEntry -> //IMPORTANT PART: getting the scoped ViewModel reference. val parentEntry = remember(backStackEntry) { navController.getBackStackEntry("Parent") } val parentViewModel = hiltViewMode

Recommends DHoward · 2023-10 · Stack Overflow · on “How to share a viewmodel between NavGraph components (only)”

If using nested nav graphs, this extension function can cause the app to crash if you navigate to this nav graph, then pop its parentBackStackEntry by navigating back. This YouTube video shows a very similar extension function, but guards against trying to read a non-existent back stack entries. [this video]

Cites zaid khan · 2023-07 · Stack Overflow · on “NavHost's composable() function calling itself again on executing navController.navigate() in jetpack compose”

that's how it works it will call the composable block again and execute functions inside it again, if you want to initialize a view model that is shared in more than one screen, inside a composable block then make sure that it's a sharedViewModel so you would get the same instance of it in all of your screens. [this video]

Cites ssantos · 2024-06 · Stack Overflow · on “Combine BottomNavigation with List - Detail navigation with animations in Jetpack compose”

I ended up using independent NavController s instead of adding nested navigation s inside a single NavHost . It looks like this.- NavHost(tabNavController, startDestination = TabNavigationDestination.Home.route) { composable(route = TabNavigationDestination.Home.route) { val homeNavController = rememberNavController() currentNavController = homeNavController NavHost(homeNavController, startDestination = NavigationDestination.UserProfile.route) { composable( NavigationDestination.UserProfile.route, enterTransition = { slideIntoContainer(AnimatedContentTransitionScope.SlideDirection.Start, tween(transitionDuration)) }, exitTransition = { slideOutOfContainer(AnimatedContentTransitionScope.Slide