You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Compose导航参数始终取默认值或null问题求助

解决Jetpack Compose导航中startDestination参数无法解析的问题

你遇到的问题是因为直接将带参数的路由字符串设为startDestination时,导航组件没有正确解析路径中的参数。以下是两种可行的解决方法:

方法一:通过navigate()初始化跳转

将起始页设为一个临时路由,在初始化后立即跳转到带参数的目标页面,确保参数被正确解析:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            TryingSomethingTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    val navController = rememberNavController()
                    NavHost(
                        navController = navController,
                        startDestination = "initial"
                    ) {
                        // 临时起始页,仅用于跳转
                        composable("initial") {
                            LaunchedEffect(Unit) {
                                navController.navigate("profile/12345") {
                                    // 跳转后移除临时页面,避免返回时回到空页面
                                    popUpTo("initial") { inclusive = true }
                                }
                            }
                        }
                        composable(
                            route = "profile/{id}",
                            arguments = listOf(
                                navArgument("id") {
                                    type = NavType.StringType
                                    defaultValue = "invalid"
                                }
                            )
                        ) { backStackEntry ->
                            val id = backStackEntry.arguments?.getString("id") ?: "invalid"
                            Log.d("MyTag", "id: $id") // 此时会输出id: 12345
                        }
                    }
                }
            }
        }
    }
}

方法二:使用currentBackStackEntryAsState获取参数

如果不想添加临时页面,也可以通过监听当前导航栈条目来获取参数,确保参数被正确读取:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            TryingSomethingTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    val navController = rememberNavController()
                    NavHost(
                        navController = navController,
                        startDestination = "profile/12345"
                    ) {
                        composable(
                            route = "profile/{id}",
                            arguments = listOf(
                                navArgument("id") {
                                    type = NavType.StringType
                                    defaultValue = "invalid"
                                }
                            )
                        ) {
                            val currentEntry = navController.currentBackStackEntryAsState().value
                            val id = currentEntry?.arguments?.getString("id") ?: "invalid"
                            Log.d("MyTag", "id: $id")
                        }
                    }
                }
            }
        }
    }
}

关键说明

  • 直接设置带参数的startDestination时,导航组件的参数解析逻辑可能未触发,导致无法读取到路径中的参数值。
  • 使用navigate()方法跳转是最可靠的传递参数方式,能确保导航组件正确解析路由中的参数。

内容的提问来源于stack exchange,提问作者Atul Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 07:16:01