Hello! Does anyone tried to implement deeplinks wi...
# compose-android
l
Hello! Does anyone tried to implement deeplinks with newer navigation type-safe args ? If so, any examples of how to do it? I left on the thread what I got so far 🧵
Copy code
composable<Route1>(
deepLinks = listOf( navDeepLink { uriPattern = "<example://compose/{id}>" } )
) {
   val (id) = it.toRoute<Route1>()
   Text(text = "Id is $id")
}
I've tried to launch this by running the command on adb shell
am start -d "<example://compose/15>" -a android.intent.action.VIEW
, and the intent filter is the following:
Copy code
<data android:scheme="example" />
<data android:host="compose" />
But throws a serialization error 🤔
s
Now wouldn't it be helpful if we also knew what Route1 looked like?
l
Indeed, I forgot to add it. Thanks:
data class Route1(val id: Int)
I think I figured out how to do it, but perhaps there is another way
s
What did you try which did work?
l
Copy code
composable<Route1>(
             deepLinks = listOf(
                 navDeepLink { uriPattern = "<example://compose/{id}>" }
             )
         ) {
             val routeInstance = it.arguments?.getInt("id")?.run { Route1(this) } ?: it.toRoute()
             Text(text = "Route is $routeInstance")
         }
I was looking to have a composable route that can be navigated from deeplinks and using a type safe route
That worked, but I don't know if there is another way (more standard perhaps)
i
You'll want to use the
navDeepLink<Route1>
API: https://androiddev.social/@ianlake/112599858226100367
🙌 1
l
Amazing! Thanks Ian. Here's the result:
Copy code
composable<Route1>(
    deepLinks = listOf(
        navDeepLink<Route1>("<example://compose/{id}>"),
    ),
) {
    val routeInstance = it.toRoute<Route1>()
    Text(text = "Route is $routeInstance")
}
s
Pretty sure the "basePath" is supposed to just be what it says, the base path, not to include anything about the args themselves. Here are some test which show examples of how it works https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:navigati[…]/java/androidx/navigation/NavDeepLinkBuilderTest.kt;l=135-156 Also take a look at the docs here https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:navigati[…]Fnavigation%2FNavDeepLinkDslBuilder.kt%20function:navDeepLink So the
{id}
part in there looks wrong.
l
Hm, I tried without the
{id}
and it didn't work. I'll take a look on that anyways