如何将 id 和应用程序传递给 Jetpack Compose 中的 viewModel/viewModelFactory?

How to pass id and application to a viewModel/viewModelFactory in Jetpack Compose?

我使用 Jetpack Compose 和导航组合,我想将 id 传递给这个 viewmodel:

class RecallViewModel(private val id:Long,application: Application):AndroidViewModel(application) {
  ............................
}

可组合函数:

我不知道如何在可组合函数中获取 application:

@Composable
fun RecallScreen(
    id:Long,
    onEnd:() -> Unit
){
       val recallViewModel = viewModel(factory = RecallViewModelFactory(
id = id,application = "i don't know how to get application"))

}

和工厂

class RecallViewModelFactory(private val id:Long,val application: Application):ViewModelProvider.AndroidViewModelFactory(application) {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return RecallViewModel(id,application) as T
    }
}

        composable(
            "${Routes.recall}/{id}",
            arguments = listOf(navArgument("id") { type = NavType.LongType })
        ) {
            RecallScreen(
                id = it.arguments!!.getLong("id"),
                onEnd = {navController.navigateUp()}
            )
        }

回答您的问题:您从 LocalContext 对象中检索 Application

val context = LocalContext.current
val application = context.applicationContext as Application

但是,使用 Navigation Compose 时,您不需要手动将任何参数传递给 ViewModel。相反,您可以利用内置的 SavedState in ViewModels and add a SavedStateHandle 参数支持您的 ViewModel。 SavedStateHandle 是一张 key/value 地图,会自动填充您目的地的参数。

这意味着您的 ViewModel 变为:

class RecallViewModel(
    application: Application,
    savedStateHandle: SavedStateHandle
):AndroidViewModel(application) {

  // Get your argument from the SavedStateHandle
  private val id: Long = savedStateHandle.get("id")

  ............................
}

而且您不再需要从参数中手动解析您的 ID 或将其传递给您的 ViewModel:

composable(
    "${Routes.recall}/{id}",
    arguments = listOf(navArgument("id") { type = NavType.LongType })
) {
    RecallScreen(
        onEnd = {navController.navigateUp()}
    )
}
@Composable
fun RecallScreen(
    onEnd:() -> Unit
) {
    val recallViewModel: RecallViewModel = viewModel()
}