View Model
View Model
● A ViewModel’s onCleared() is called only when the app is put into the
background and the app process is killed in order to free up the
system’s memory.Therefore, lifecycle changes of activities and
fragments does not affect to their ViewModels.(Activities and
fragments may destroy and recreate, but view model will live
throughout the process)
Create a ViewModelFactory.
We provide that extra support by creating a Factory class and passing its instance to the
ViewModelProvider.
It will be called when the app is put into the background and the app process is killed
in order to free up the
system's memory.
When the user invokes finish() of an activity, its view model will be cleared().
Also when we click on the back button, ViewModel of current activity will be cleared
(onCleared() will be invoked)