I use Jetpack Compose and navigation compose and I want to pass id to this viewmodel
:
class RecallViewModel(private val id:Long,application: Application):AndroidViewModel(application) {
............................
}
Composable function:
I don't know how to get the application
in composable function:
@Composable
fun RecallScreen(
id:Long,
onEnd:() -> Unit
){
val recallViewModel = viewModel(factory = RecallViewModelFactory(
id = id,application = "i don't know how to get application"))
}
and factory
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()}
)
}
id
as anavArgument
to your screen as part of your route? If so, it is already available to your ViewModel via theSavedStateHandle
that is supported out of the box. Is there a particular reason you're trying to build a manual factory for this?composable
destination you use for yourRecallScreen
?