首页 > 解决方案 > 将 URL 作为参数传递给 Jetpack Compose Navigation

问题描述

HistoryDetail在我的应用程序中为屏幕创建了一个目的地。

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

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

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

我收到illegalArgumentException以下消息。

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("HistoryDetail/test")

标签: androidandroid-jetpack-compose

解决方案


导航路线相当于 url。一般来说,你应该通过类似id那里的东西。

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

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

推荐阅读