I'm going through this codelab: https://developer.android.com/codelabs/android-lifecycles#6 It explains how to use SavedStateHandle in a ViewModel to survive process death. The constructor of the ViewModel is as below:
private SavedStateHandle mState;
public SavedStateViewModel(SavedStateHandle savedStateHandle) {
mState = savedStateHandle;
}
And the viewmodel is initialized in the Activity as follows:
mSavedStateViewModel = new ViewModelProvider(this).get(SavedStateViewModel.class);
When is the ViewModel's constructor called? And how does the Viewmodel get the savedStateHandle
parameter of the ViewModel?
Edit:
I found the answer to my question through this blogpost: https://www.rockandnull.com/viewmodel-savedstate/
It says that if theSavedStateHandle
is the only parameter in our viewmodel's constructor the by viewModels
delegate provides it to the viewmodel automatically.
However, If we have a combination of custom parameters that provided by the dependency injection framework(Hilt) e.g: the repository, a couple of other runtime arguments e.g: selected category ID and the SavedStateHandle to help us survive the process death in the viewmodel's constructor - How can we provide all those parameters to the factory?
Please post a small example for my understanding