https://kotlinlang.org logo
#android
Title
# android
r

Rohan Maity

08/14/2022, 4:26 AM
Hello, I am trying the navigation kotlin dsl (not jetpack compose one) , in here, I am having issues while sending optional arguements to the route and I cannot find any documentation for it as well https://developer.android.com/guide/navigation/navigation-kotlin-dsl UPDATE Solution to this is the same as compose navigation optional args, so one can refer that
✔️ 1
r

Rohan Maity

08/14/2022, 4:39 AM
This does not tell how to pass optional args for example Query params may or may not come, but Path params must exist for proper routing
i

Ian Lake

08/14/2022, 5:48 AM
It sounds like you've already figured it out? You either include them in the route when you call navigate or you don't. It is just a string
r

Rohan Maity

08/14/2022, 6:01 AM
yes I figured it out. I did not try with omitting string, but here is what i found out When you build the route for fragmentDestination like this
Copy code
fragment<F>(route)
then this route should be format
Copy code
"baseRoute/{permanent_arg1_name}/{permanent_arg_2_name}?optionalArg1Name={optionalArg1Name}&optionalArg2Name={optionalArg2Name}"
This because when fragment destination is built, it uses the argument name to build the route and when you route using navigate(), one has to pass the values for placeHolder so that they can match against values with which route has been created Notice how the name as been used for optionalArgs as well this because and argument is built with this very name in fragment dsl, so when building route, use the very same name for placeHolders even in optionalArgs (just like normal args)
i

Ian Lake

08/14/2022, 6:03 AM
Well, no. the
optionalArg1Name=
could be any key; Nothing cares what that key is
I had a stack overflow answer about this: https://stackoverflow.com/a/65544413/1676363
r

Rohan Maity

08/14/2022, 6:04 AM
I am not talking about key , I ma taking about the plaecHolder values ones in
{}
i

Ian Lake

08/14/2022, 6:04 AM
which points out that the routes follow the exact same rules as any implicit deep link: https://developer.android.com/guide/navigation/navigation-deep-link#implicit
Which documents exactly how placeholders, path parameters, and query parameters all work
We do explicitly call this out in the Navigation Compose docs (that link between routes and implicit deep links for how arguments work): https://developer.android.com/jetpack/compose/navigation#nav-with-args
But perhaps what you're missing was that same type of thing on the Kotlin DSL page?
r

Rohan Maity

08/14/2022, 6:13 AM
My point was about the placeHolder values only like if fragment is build like this in navigation kotlin dsl (not compose one)
Copy code
fragment<PlantDetailFragment>("${nav_routes.plant_detail}?plant_id="{${nav_arguments.plant_id}}") {
    label = resources.getString(R.string.plant_detail_title)
    argument(nav_arguments.plant_id) {
        type = NavType.StringType
    }
}
then the name used in argument building should match the one in fragment destination route building in placeHolders (ones in curly braces
{}
) so that arguments when passed during navigating using
navigate()
could match the proper placeholders
i

Ian Lake

08/14/2022, 6:14 AM
The
${random_kotlin_code}
syntax is part of Kotlin, you're not actually putting any braces in your actual string in that code
And the Compose DSL is literally the same DSL. There is only one DSL 🙂
the
fragment
method is a different method than the
composable
one though
r

Rohan Maity

08/14/2022, 6:16 AM
The
${random_kotlin_code}
syntax is part of Kotlin, you're not actually putting any braces in your actual string in that code
I am sorry I added them now here is the updated string
"${nav_routes.plant_detail}?plant_id="{${nav_arguments.plant_id}}"
Notice the extra
{}
it was after this navigation dsl works
i

Ian Lake

08/14/2022, 6:19 AM
Yes, that is how you do optional arguments in both implicit deep links and routes - using query parameters
👍 1
r

Rohan Maity

08/14/2022, 6:22 AM
And the Compose DSL is literally the same DSL. There is only one DSL 🙂
Yes, after going through compose, one can infer this, but IMHO, • there should have been section of optional args in Navigation kotlin dsl (not compose one) or atleast a link referring to compose doc. • Also documentation of Navigation kotlin dsl is kinda incorrect here: https://developer.android.com/guide/navigation/navigation-kotlin-dsl#navgraphbuilder. It does not tell about those extra
{}
I agree so far people have figured out on their own or through compose, but I think this should be fixed as well
i

Ian Lake

08/14/2022, 6:24 AM
Makes sense to me! Can you file a docs issue and include a link to it here? https://issuetracker.google.com/issues/new?component=192697
I suspect that second part is more a simple oversight in the code snippet than anything intentionally left out
👍 1
1
r

Rohan Maity

08/14/2022, 6:27 AM
Sure will file a ticket on Issue tracker Also one more thing, I am not sure but I heard from somewhere that one can contribute to android documentation as well, If this is still the case, can I contribute here?
i

Ian Lake

08/14/2022, 6:29 AM
Javadoc, yes, guides on developer.android.com, no, unfortunately
r

Rohan Maity

08/14/2022, 6:29 AM
Ok got it. Will file a create a ticket only then