Hello, I am trying the navigation kotlin dsl (not ...
# android
r
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
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
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
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
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
I am not talking about key , I ma taking about the plaecHolder values ones in
{}
i
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
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
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
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
Yes, that is how you do optional arguments in both implicit deep links and routes - using query parameters
👍 1
r
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
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
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
Javadoc, yes, guides on developer.android.com, no, unfortunately
r
Ok got it. Will file a create a ticket only then