androidandroid-jetpackandroid-jetpack-composeandroid-architecture-navigation

How do you pass optional boolean arguments in Android Jetpack Compose?


I'm trying to display a welcome message on the Dashboard to the user when he can from the welcome page. So, in my route for the Dashboard, I defined a optional argument.

It's working fine to access it without any argument (because, it's optional) but when I add one, it's not working anymore.

       composable(Routes.welcome) {
            WelcomeScreen {
                navController.navigate("dashboard?isWelcome={true}")
            }
        }

        composable(
            "dashboard?isWelcome={isWelcome}}",
            arguments = listOf(
                navArgument("isWelcome") {
                    type = NavType.BoolType
                }
            )
        ) {
            val isFromWelcome = it.arguments?.getBoolean("isWelcome") ?: false
...
}

The error I have :

Navigation destination that matches request NavDeepLinkRequest{ uri=android-app://androidx.navigation/dashboard?isWelcome={true} } cannot be found in the navigation graph

Thanks for your advice :)


Solution

  • Sending boolean

    val booleanValue = true
    navController.navigate("my_destination?my_arg_id=${booleanValue}")
    

    Receiving boolean

    composable(
        route = "my_destination?my_arg_id={my_arg_id}",
        arguments = listOf(navArgument("my_arg_id") { defaultValue = false })
    ) { from ->
        val myBooleanArg = from.arguments?.getBoolean("my_arg_id")
        MySecondScreen(myBooleanArg)
    }