Since Lifecycle 2.5.0 there are official viewModel
methods for doing exactly the same. This library is discontinued.
Use a simple syntax to manually create new ViewModel instances and provide all parameters directly instead of using SavedStateHandle.
The usage looks like this:
val mainViewModel by viewModel {
MainViewModel(args)
}
Here args
is a reference of some arbitrary type you wanna pass into the MainViewModel.
If you need to use SavedStateHandle for saving state inside a ViewModel use savedStateViewModel
instead:
val savedStateMainViewModel by savedStateViewModel { savedStateHandle ->
SavedStateMainViewModel(args, savedStateHandle)
}
These methods are provided as extensions for ViewModelStoreOwner, which means they can be with Activity, Fragment or NavBackStackEntry as a receiver. Or simply inside you Activity or Fragment classes.
You may also find it convenient to use these methods together with Dagger and @AssistedInject. You may find the samples for Dagger/Hilt/Anvil in my navigation library project here: samples-di.
Note: As it is intended as an alternative to SavedStateHandle for passing parameters into a ViewModel, SavedStateHandle instances are not pre-filled with arguments from Activity, Fragment and NavBackStackEntry.
There are similar methods for you to use inside your composables:
val composeViewModel = viewModel {
ComposeViewModel(args)
}
val savedStateComposeViewModel = savedStateViewModel { savedStateHandle ->
SavedStateComposeViewModel(args, savedStateHandle)
}
Just add:
implementation("dev.olshevski.viewmodel:easy-factories:1.0.0")
For Compose use this instead:
implementation("dev.olshevski.viewmodel:easy-factories-compose:1.0.0")