Arun
03/19/2021, 11:41 AMval exampleViewModel = hiltNavGraphViewModel<ExampleViewModel>()
However, I couldn’t find anything or any info on passing a ViewModelProvider.Factory above (in case there are parameters for the ViewModel constructor).
Am I missing something or is it yet to be developed?Lucien Guimaraes
03/19/2021, 11:47 AMArun
03/19/2021, 12:17 PMviewModel() instead right?Gabriel
03/19/2021, 12:36 PMcomposable(
"${Screen.PlayerScreen.route}/{id}",
arguments = listOf(navArgument("id") { type = NavType.IntType })
) {
val arguments = requireNotNull(it.arguments)
val playersViewModel = viewModel<PlayersViewModel>(
factory = PlayersViewModelFactory(
it,
PlayersRepository(),
arguments
)
)
PlayersScreen(playersViewModel)
}Arun
03/19/2021, 1:15 PMviewModel(). My ViewModel has few dependencies in its constructor that Hilt is supposed to provide. Using hiltNavGraphViewModel inside composable destination just works out of the box. However, if I use simply viewModel() , it throws Runtime exception that it can’t create an instance of the ViewModel.Arun
03/19/2021, 1:16 PMviewModel(). 🤔Lucien Guimaraes
03/19/2021, 1:20 PMLucien Guimaraes
03/19/2021, 1:20 PMIan Lake
03/19/2021, 3:59 PMhiltNavGraphViewModel() does - automatically uses the right factory for you. If you're not using Hilt, then use viewModel() and pass it your custom factory, which it takes as an optional parameterArun
03/19/2021, 5:36 PMallan.conda
04/05/2021, 3:35 AMThehttps://developer.android.com/jetpack/compose/libraries#hiltfunction mentioned in the ViewModel section automatically uses the ViewModel that Hilt constructs with theviewModel()annotation. We’ve provided documentation with information about Hilt's ViewModel integration.@HiltViewModel
allan.conda
04/05/2021, 3:40 AMIf yourannotated@HiltViewModelis scoped to the navigation graph, use theViewModelcomposable function that works with fragments or activities that are annotated withhiltNavGraphViewModel.@AndroidEntryPoint
Ian Lake
04/05/2021, 3:40 AM