Hey everyone, I have a question about retaining st...
# compose-destinations
a
Hey everyone, I have a question about retaining state and managing multiple backstacks (multi module project) in v2. I'll share my setup in this threadβ€”can anyone help me understand why the state isn't being restored for my screens?
App module:
Copy code
@NavHostGraph
annotation class MainGraph {
    @ExternalNavGraph<ExploreNavGraph>(
        start = true
    )
    @ExternalNavGraph<MyLibraryNavGraph>
    @ExternalNavGraph<SettingsNavGraph>
    companion object Includes
}
Explore module:
Copy code
@NavGraph<ExternalModuleGraph>
internal annotation class ExploreGraph

@Destination<ExploreGraph>(
    start = true,
)
@Composable
fun ExploreRoute(
MyLibrary module:
Copy code
@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:
Copy code
val destinationNavigator = appState.navController.rememberDestinationsNavigator()
...
destinationsNavigator.navigate(direction = destination.direction) {
    launchSingleTop = true
    restoreState = true
}
DestinationNavHost setup:
Copy code
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,
                )
            },
        )
    }
}
If I add this check:
Copy code
if (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:
Copy code
/**
 *
 * * πŸ—ΊοΈ[MainGraph]
 * * βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™β†³πŸ—ΊοΈπŸ[ExploreNavGraph] 🧩
 * * βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™ ☝️ to see contents 
 * * βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™β†³πŸ—ΊοΈ[MyLibraryNavGraph] 🧩
 * * βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™ ☝️ to see contents 
 * * βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™β†³πŸ—ΊοΈ[SettingsNavGraph] 🧩
 * * βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™βˆ™ ☝️ to see contents 
 */