相关疑难解决方法(0)

使用撰写导航传递 Parcelable 参数

我想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().

有没有办法要么序列化ParcelableString,所以我可以通过它的路线或通过导航参数作为比其他函数的对象navigate(route: String)

android kotlin android-jetpack-navigation android-jetpack-compose

13
推荐指数
5
解决办法
2672
查看次数

喷气背包组合导航的多个参数

如何声明具有多个导航参数的导航路线?我查了资料,并且所有 这些物品(这似乎只是重申文件说什么),我只能找到途径的例子有一个参数。

这是我所拥有的:

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)

我为上面的例子随意选择了一个逗号分隔的语法来代替我正在寻找的真实语法。

所以,我的问题是:在声明导航路线时,多个参数的正确语法是什么?(可选参数呢?)

android android-jetpack-navigation android-jetpack-compose

4
推荐指数
2
解决办法
907
查看次数