Trying to understand what is the difference with using ViewModel to save some state of activity or fragment and save them using the savedInstanceState package.
It seems that the ViewModel instance is kept alive when the activity / fragment is destroyed by os in case of a configuration change, so when os recreates the activity / fragment, you can get data from the ViewModel instance that is still valid.
Is this used to minimize the application and reopen it?
Some test seems to minimize the application and reopen the application, os recreates the activity / fragment with the package stavedInstanceState in onCreate () is not null (regardless of what is called when onSaveInstanceStae () is called). But the ViewModel has been cleared, so a new instance is created without previous data.
Does this mean that in this case, os can get the saved state of the instance and go to the onCreate () activity / fragment, but the ViewModel should be a new instance without the data of the previous instance, or should the viewModel have some additional step for storing / restoring the data that crosses the instances?
source
share