我想BluetoothDevice使用组合导航将 Parcelable 对象 ( ) 传递给可组合对象。
传递原始类型很容易:
composable(
"profile/{userId}",
arguments = listOf(navArgument("userId") { type = NavType.StringType })
) {...}
Run Code Online (Sandbox Code Playgroud)
navController.navigate("profile/user1234")
Run Code Online (Sandbox Code Playgroud)
但是我不能在路由中传递一个parcelable 对象,除非我可以将它序列化为一个字符串。
composable(
"deviceDetails/{device}",
arguments = listOf(navArgument("device") { type = NavType.ParcelableType(BluetoothDevice::class.java) })
) {...}
Run Code Online (Sandbox Code Playgroud)
val device: BluetoothDevice = ...
navController.navigate("deviceDetails/$device")
Run Code Online (Sandbox Code Playgroud)
上面的代码显然不起作用,因为它只是隐式调用toString().
有没有办法要么序列化Parcelable的String,所以我可以通过它的路线或通过导航参数作为比其他函数的对象navigate(route: String)?
android kotlin android-jetpack-navigation android-jetpack-compose
如何声明具有多个导航参数的导航路线?我查了资料,并且所有 的 这些物品(这似乎只是重申文件说什么),我只能找到途径的例子有一个参数。
这是我所拥有的:
composable(
route = "createExercise/{exerciseId}",
arguments = listOf(navArgument("exerciseId") { type = NavType.IntType })
) { backStackEntry ->
CreateExerciseScreen(
exerciseId = backStackEntry.arguments!!.getInt("exerciseId"),
)
}
Run Code Online (Sandbox Code Playgroud)
这是我想要的:
composable(
route = "createExercise/{exerciseId},{workoutId}",
arguments = listOf(
navArgument("exerciseId") { type = NavType.IntType },
navArgument("workoutId") { type = NavType.IntType },
)
) { backStackEntry ->
CreateExerciseScreen(
exerciseId = backStackEntry.arguments!!.getInt("exerciseId"),
workoutId = backStackEntry.arguments!!.getInt("workoutId"),
)
}
Run Code Online (Sandbox Code Playgroud)
我为上面的例子随意选择了一个逗号分隔的语法来代替我正在寻找的真实语法。
所以,我的问题是:在声明导航路线时,多个参数的正确语法是什么?(可选参数呢?)