Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewModel vs static variable in an activity

Tags:

android

I know that one of the solutions to handle orientation change is to use ViewModel to hold the data as it outlives the activity.

However, I was wondering, can't we achieve the same thing if we create a static variable in the class to hold the same data?

Why would we go down the ViewModel path?

Thanks

like image 488
Snake Avatar asked Sep 21 '18 05:09

Snake


1 Answers

One reason is because Activities themselves aren't static or singletons. You can have an ItemViewActivity launch another ItemViewActivity for a different item, and if they're sharing the same variables via statics you've probably just broke the back button. And if you don't code them very carefully you're probably going to send data to the wrong Activity's views.

like image 106
Gabe Sechan Avatar answered Nov 15 '22 17:11

Gabe Sechan