Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 826
Jérémie Guillot Avatar asked Mar 24 '26 20:03

Jérémie Guillot


1 Answers

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)
}

2024 Update: You can use the Type Safe navigation now. More information about it here

like image 134
Merkost Avatar answered Mar 26 '26 09:03

Merkost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!