How do I construct a URI that allows me to navigat...
# compose
j
How do I construct a URI that allows me to navigate to a destination that supports deeplinks? I have a destination that looks like this:
Copy code
composable(
    route = Destination.Route,
    deepLinks = listOf(
        navDeepLink { uriPattern = "/path1" },
        navDeepLink { uriPattern = "/path1/{argument1}" },
        navDeepLink { uriPattern = "/path2/{argument2}" },
    ),
)
So when I click on a link like https://www.test.com/path1/123 the app opens the destination correctly, however when I try to do the same thing programmatically by doing:
Copy code
navController.navigate(Uri.parse(URLEncoder.encode("<https://www.test.com/path1/123>", "UTF-8"))
I get a
Copy code
java.lang.IllegalArgumentException: Navigation destination that matches request NavDeepLinkRequest{ uri=https%3A%2F%2Ftest.com%2Fpath1%2F123 } cannot be found in the navigation graph
any idea what I might be doing wrong here?
s
maybe you should remove the protocol + host (
<https://www.test.com/>
) and only keep the path?
j
I tried that already but it doesn’t work
s
looking at navigation docs, maybe it's the opposite?
try including the host in the
deepLinks
uri patterns
From the docs:
Copy code
val uri = "<https://www.example.com>"

composable(
    "profile?id={id}",
    deepLinks = listOf(navDeepLink { uriPattern = "$uri/{id}" })
) { backStackEntry ->
    Profile(navController, backStackEntry.arguments?.getString("id"))
}
☝️ 1
i
Your
Uri.parse(URLEncoder.encode(
also is very suspect - generally you just pass the string to
Uri.parse
- encoding it ahead of time is actually stripping all of the
/
and other symbols from the Uri (which is why you see
https%3A%2F%2F
for instance). I don't know why you'd encode the whole string (if anything, you'd use
Uri.encode
around a single argument)
s
@Ian Lake makes sense there's also a
String.toUri()
extension function in androidx, much simpler
j
I was already adding the host to the `uriPattern`s, I just omitted that for brevity. I checked the link provided by @Stylianos Gakis and noticed the validation where even if the uri string doesn’t have the scheme the resulting
NavDeepLink
does have it, so I added
"https://"
to the `uriPattern`s and now it works, however the uri that I pass to
navController.navigate()
has to be parsed from a string matching exactly that pattern, so it has to be
"<https://www.test.com/path1>"
, it can’t be
"<http://test.com/path1|test.com/path1>"
or
"<http://www.test.com/path1|www.test.com/path1>"
, which kinda weirds me out because in the test provided by Stylianos the uri string doesn’t have the scheme, is this expected?
i
As per the docs https://developer.android.com/guide/navigation/design/deep-link#implicit
URIs without a scheme are assumed as either http or https. For example, www.google.com matches both http://www.google.com and https://www.google.com.
148 Views