https://kotlinlang.org logo
p

pourpre

11/04/2020, 5:44 PM
Does anyone know of any samples using navigation-compose specifically the passing and receiving of arguments? The current samples don't use arguments.
p

pourpre

11/04/2020, 6:25 PM
Thanks for the link. String manipulation does not seem like a particularly Kotlin thing to do, I was hoping for something that took advantage of the arguments parameter in composable. eg.
composable(route=MY_ROUTE, arguments = listOf(
navArgument("arg1"){
defaultValue = "DEFAULT"
nullable = true
}) { backStackEntry -> MyRouteComposable(args=backStackEntry.arguments) }
and
navController.navigate(route=MY_ROUTE, args=bundleOf("arg1" to "param1"))
i

Ian Lake

11/04/2020, 6:33 PM
As per the docs (https://developer.android.com/jetpack/compose/navigation#nav-with-args) and previous discussions here, this isn't how routing works in Navigation Compose. Just like a web URL, the arguments are part of the route - part of the very definition of where you are going
So
profile/{profileId}
is a route to a specific profile
The testing part of that same doc explains how you should be removing any navigation code from your actual composable itself. Instead, pass parsed, type safe arguments into your composable and type safe on click listeners that you then transform into navigate calls
p

pourpre

11/04/2020, 7:12 PM
Cheers for that. I was focusing so much on the source code, I forgot about the online docs. Is it my imagination or is the documentation about optional arguments wrong?
Copy code
composable(
    "profile?userId={userId}",
    arguments = listOf(navArgument("userId") { defaultValue = "me" })
) { backStackEntry ->
    Profile(navController, backStackEntry.arguments?.getString("userId"))
}
Shouldn't this be (the route name with no arguments)
Copy code
composable(
    "profile",
    arguments = listOf(navArgument("userId") { defaultValue = "me" })
) { backStackEntry ->
    Profile(navController, backStackEntry.arguments?.getString("userId"))
}
i

Ian Lake

11/04/2020, 7:21 PM
you still need to pass all arguments via the route, optional or not
2 Views