Jetpack Compose + Navigation: navigating to to BottomBar destination from different BottomBar screen

Viewed 432

Problem:

BottomBar has 4 destination :ScreenA, ScreenB, ScreenC and ScreenD.

While on ScreenA pressing a button(not bottom bar item) I want to go to ScreenB with parameter.

Code:

NavGraph

AnimatedNavHost(navController = navController, startDestination = "screen_a") {
    composable("screen_a") {
        ScreenA(navToScreenB = {
            navController.navigate("screen_b?param=1") {
        })
    }
    navigation(
        route = "screen_b",
        startDestination = "screen_b?param={param}",
    ) {
        composable(
            route = "screen_b?param={param}",
            arguments = listOf(navArgument("param") { type = NavType.IntType })
        ) { backStackEntry ->
            val param =  backStackEntry.arguments?.getBoolean("param") ?: 0
            ScreenB(param = param)
        }
        composable(route = "screen_b/details") {
            ScreenB_details(...)
        }
    }
}

BottomBar

fun BottomNavigation(navController: NavController, selectedNavigation: Screen) {
    val items = listOf(...)
    BottomNavigation(
        backgroundColor = MaterialTheme.colors.primary
    ) {
        items.forEach { item -> BottomNavigationItem(
                onClick = {
                    navController.navigate(item.screen.route) {
                        navController.graph.startDestinationRoute?.let { route ->
                            popUpTo(route) { saveState = true }
                        }
                        launchSingleTop = true
                        restoreState = true
                    }
                }
            )
        }
    }
}

So far clicking the button on ScreenA navigates me to ScreenB but the param value is always 0 as I would never pass the arg which is passed in navToScreenB lambda. I'm using compose_version = '1.2.0-alpha04' and "androidx.navigation:navigation-compose:2.5.0-alpha03"

1 Answers

Turns out that {param} in route and navArgument({param}) have to be exact the same even if the path looks correct and has the args in route

Related