Navigation in navigation drawer (with popbackstack...
# compose-android
v
Navigation in navigation drawer (with popbackstack with restore and save) not working when navigated through deep linking. I have a drawer screen. and for each navigation option in drawer screen, i have a separate navhost to maintain backstack for each section. When i navigate to a inner screen through deep link, if i click on drawer option the drawer screen does not navigate to another navHost. Code in 🧵 #navigation #deeplink
Here is my navigation drawer code...
Copy code
@Composable
fun DrawerScreen(modifier: Modifier = Modifier) {

    val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
    var navController = rememberNavController()
    val coroutineScope = rememberCoroutineScope()
    val currentDestinationRoute = navController.currentBackStackEntryAsState().value?.destination?.route

    ModalNavigationDrawer(
        drawerContent = {
            ModalDrawerSheet {
                NavigationDrawerItem(label = { Text("Section 1") }, selected = currentDestinationRoute?.contains("Section1") ?: false , onClick = {
                    coroutineScope.launch {
                        drawerState.close()
                    }.invokeOnCompletion {
                        navController.navigate("Section1", navOptions = navOptions {
                            popUpTo(navController.graph.findStartDestination().id) {
                                saveState = true
                            }
                            launchSingleTop = true
                            restoreState = true
                        })
                    }
                })
                NavigationDrawerItem(label = { Text("Section2") }, selected = currentDestinationRoute?.contains("Section2") ?: false, onClick = {
                    coroutineScope.launch {
                        drawerState.close()
                    }.invokeOnCompletion {
                        navController.navigate("Section2", navOptions = navOptions {
                            popUpTo(navController.graph.findStartDestination().id) {
                                saveState = true
                            }
                            launchSingleTop = true
                            restoreState = true
                        })
                    }
                })
            }
        },
        drawerState = drawerState
    ) {
        NavHost(navController = navController, startDestination = "Section1") {
            composable(route = "Section1",  deepLinks = listOf( navDeepLink { uriPattern = "<https://www.example.com/section/1/screen>" })) {
                val section1Controller = rememberNavController()
                NavHost (navController = section1Controller, startDestination = "Section1Screen1") {
                    composable("Section1Screen1") { backStackEntry ->
                        FullScreenScaffold("Section 1 - Screen 1", drawerState = drawerState, onNavigate = {section1Controller.navigate("Section1Screen2")})
                    }
                    composable("Section1Screen2") {
                        FullScreenScaffold("Section 1 - Screen 2", drawerState = drawerState, onNavigate = {section1Controller.navigate("Section1Screen3")})
                    }
                    composable("Section1Screen3", deepLinks = listOf( navDeepLink { uriPattern = "<https://www.example.com/section/1/screen>" })) { backStackEntry ->
                        FullScreenScaffold("Section 1 - Screen 3", drawerState = drawerState, onNavigate = {})
                    }
                }
            }

            composable(route = "Section2", deepLinks = listOf( navDeepLink { uriPattern = "<https://www.example.com/section/2/screen>" })) {
                val section2Controller = rememberNavController()
                NavHost(navController = section2Controller, startDestination = "Section2Screen1") {
                    composable("Section2Screen1") {
                        FullScreenScaffold("Section 2 - Screen 1", drawerState = drawerState, onNavigate = {section2Controller.navigate("Section2Screen2")})
                    }
                    composable("Section2Screen2") {
                        FullScreenScaffold("Section 2 - Screen 2", drawerState = drawerState, onNavigate = {section2Controller.navigate("Section2Screen3")})
                    }
                    composable("Section2Screen3", deepLinks = listOf( navDeepLink { uriPattern = "<https://www.example.com/section/2/screen>" })) { backStackEntry ->
                        FullScreenScaffold("Section 2 - Screen 3", drawerState = drawerState, onNavigate = {})
                    }
                }
            }
        }
    }
}