将 URL 作为参数传递给 Jetpack Compose Navigation [英] Passing URL as a parameter to Jetpack Compose Navigation

查看:95
本文介绍了将 URL 作为参数传递给 Jetpack Compose Navigation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中为 HistoryDe​​tail 屏幕创建了一个目的地.

I have created a destination for HistoryDetail screen in my app.

composable(
    route = "HistoryDetail/{webpage}",
    arguments = listOf(
        navArgument("webpage") {
            type = NavType.StringType
        }
    ),
) { entry ->
    val text = entry.arguments?.getString("webpage") ?: ""
}

当我尝试通过调用导航到该屏幕时:

When I try to navigate to that screen by calling:

navController.navigate("HistoryDetail/http://alphaone.me/")

我收到带有以下消息的 illegalArgumentException.

I'm getting illegalArgumentException with the below message.

java.lang.IllegalArgumentException: Navigation destination that matches request NavDeepLinkRequest{ uri=android-app://androidx.navigation/HistoryDetail/http://alphaone.me/ } cannot be found in the navigation graph NavGraph(0x0) startDestination={Destination(0x78c9ba0c) route=Home}

如果我调用它会起作用:navController.navigate("HistoryDe​​tail/test").

It works if I call: navController.navigate("HistoryDetail/test").

推荐答案

导航路线相当于 url.通常你应该在那里传递类似 id 的东西.

Navigation routes are equivalent to urls. Generally you're supposed to pass something like id there.

当你需要在另一个 url 中传递一个 url 时,你需要对其进行编码:

When you need to pass a url inside another url, you need to encode it:

val encodedUrl = URLEncoder.encode("http://alphaone.me/", StandardCharsets.UTF_8.toString())
navController.navigate("HistoryDetail/$encodedUrl")

这篇关于将 URL 作为参数传递给 Jetpack Compose Navigation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆