Wednesday 15 January 2020

How do I use the new Saved State Module of ViewModel

I'm using lifecycle version 2.2.0-rc03 and the official docs and articles found don't even list the correct class name or constructor arguments. I think I have to get the ViewModel instance through something like this

viewModel = ViewModelProvider(this, SavedStateViewModelFactory(requireActivity().application, savedStateRegistryOwner))
            .get(SelectedTracksViewModel::class.java)

but I can't figure out the SavedStateRegistryOwner.

Can someone give a simple example of how to create the saved state ViewModel instance and the correct way to save and restore a value in the ViewModel?



from How do I use the new Saved State Module of ViewModel

No comments:

Post a Comment