Alex Char
03/24/2025, 12:54 PMAlex Char
03/24/2025, 12:54 PM@NavHostGraph
annotation class MainGraph {
@ExternalNavGraph<ExploreNavGraph>(
start = true
)
@ExternalNavGraph<MyLibraryNavGraph>
@ExternalNavGraph<SettingsNavGraph>
companion object Includes
}
Explore module:
@NavGraph<ExternalModuleGraph>
internal annotation class ExploreGraph
@Destination<ExploreGraph>(
start = true,
)
@Composable
fun ExploreRoute(
MyLibrary module:
@NavGraph<ExternalModuleGraph>
internal annotation class MyLibraryGraph
@Destination<MyLibraryGraph>(
start = true,
)
@Composable
fun MyLibraryRoute() {
MyLibraryScreen()
}
@Composable
internal fun MyLibraryScreen() {
val scrollableState = rememberLazyListState() // <- not restored
LazyColumn(
modifier = Modifier.fillMaxSize(),
contentPadding = PaddingValues(vertical = 16.dp),
state = scrollableState,
) {
repeat(100) {
item(key = it) {
Text(text = it.toString())
}
}
}
}
This is how I navigate on bottom bar item click:
val destinationNavigator = appState.navController.rememberDestinationsNavigator()
...
destinationsNavigator.navigate(direction = destination.direction) {
launchSingleTop = true
restoreState = true
}
DestinationNavHost setup:
ModalBottomSheetLayout(
bottomSheetNavigator = bottomSheetNavigator,
) {
Column(
modifier = Modifier
.padding(
top = padding.calculateTopPadding(),
)
.fillMaxSize(),
) {
DestinationsNavHost(
navGraph = NavGraphs.main,
navController = appState.navController,
dependenciesContainerBuilder = {
ExploreDependencies(
destinationNavigator = destinationNavigator,
)
MyLibraryDependencies(
destinationNavigator = destinationNavigator,
)
},
)
}
}
Alex Char
03/24/2025, 2:07 PMif (isCurrentDestOnBackStack) {
// When we click again on a bottom bar item and it was already selected
// we want to pop the back stack until the initial destination of this bottom bar
// item
destinationsNavigator.popBackStack(destination.direction, false)
return@NavigationBarItem
}
before calling destinationsNavigator.navigate
, the state is restored for ExploreNavGraph (which is root) and for MyLibraryNavGraph only if i go from SettingsNavGraph and back and forth.
Maybe my graph is wrong:
/**
*
* * πΊοΈ[MainGraph]
* * βββββββββ³πΊοΈπ[ExploreNavGraph] π§©
* * ββββββββββββββββ βοΈ to see contents
* * βββββββββ³πΊοΈ[MyLibraryNavGraph] π§©
* * ββββββββββββββββ βοΈ to see contents
* * βββββββββ³πΊοΈ[SettingsNavGraph] π§©
* * ββββββββββββββββ βοΈ to see contents
*/