ViewModel vs Activity member variable?

Viewed 179

Going by this image, the lifecycle of a ViewModel is pretty much like that of an Activity instance, so why not just use an activity instance variable? Is a ViewModel better because of the automatic call to onClear? Or because it is observable (in which case why not just have observable, normal member variables)? enter image description here

1 Answers

Maybe the graphic is not clear enough but the printed lifecycle hooks are from the activity, the viewmodel remains the same until the activity is completely finished.

So, a variable in the activity instance would be released after rotation for example as that activity instance is going to be destroyed while a new activity instance is created. But this would not happen if it's on the ViewModel as both would use the same one.

Related