ViewModel in Android
ViewModel is a class that maintains the state of the reference based on view’s lifecycle. Here View is Activity/Fragment.
Overview:
To understand the concept of viewmodel, we are going to develop a simple random number generator application(App Name: Fluky).
When Fluky open’s , It loads a random number and shows in the textview.
Get Raja kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s start to build Fluky:
Launcher Activity is created with the name of LauncherActivity.java. Next, A new java class is created with the name RandomNumberGenerator.java.
The RandomNumberGenerator.java have a method startSuffleTheNumber(),It returns the generated random number in the constructor. Check out the below code
In the LauncherActivity.java, create a reference for RandomNumberGenerator class and call the startSuffleTheNumber().
Next, the returned random number is shown in the textview. Now Fluky is ready, when we open the app we have the output similar to below image.
Now the problem with Fluky is, when the user orientation changes or any other lifecycle event changes, the LauncherActivity.java activity is recreated, so it create new instance of RandomNumberGenrator class and display the newly generated random number. Let’s upgrade Fluky by showing the screen orientation. Another TextView is added to show screen orientation, In LauncherActivity.java ,getResources().getConfiguration()
Methods is used to get the device orientation and set that orientation in Textview
Now the updated fluky output looks like below Image.
The below image shows Activity Lifecycle flow when configuration changes occurred,
When the screen orientation changes, the Activity is recreated so the data which present in UI is loads again. Random number is small process, Consider loading data from network requires more memory, and time consuming process too. So Instead of creating the new reference every time, Just use the same data which is already present. So let’s update Fluky , to stop generating new random number during configuration change.
ViewModel is class in android, which hold the data based on UI lifecycle. To create new ViewModel, just extends the class with ViewModel class
In flucky, The RandomNumberGenetator.java class is extends with ViewModel. So custom viewmodel is created for fluky.
Object creation is little different for ViewModel. Check out the below code of LauncherActivity.
The ViewModelProviders.of(LauncherActivity.java), so the LauncherActivity is the view here, RandomNumberGenerator viewmodel will work based on LauncherActivity. When the device orientation is changed the activity recreates, during recreation, Instead of creating new instance of RandomNumberGenerator, the ViewModelProviders gives the existing instance. So the existing instance have the Random number which is generated previously.
The below image represents the view lifecycle along with the viewmodel states
The android system will maintain the same ViewModel scope,until the Activity/Fragments are destroy.
Now Flucky will not generate new random number during configuration changes.
Tip: Call onCleared() callback, to manually clear the data present in the ViewModel or else once the view (Activity/Fragment) is destroyed ViewModel data is cleared automatically.
In closing, To create custom ViewModel extends your class with ViewModel class. ViewModelProviders.of() is used to create the ViewModel reference, it will gives the existing reference instead of creating new every time. onCleared() method is used to clear the data present in ViewModels
Thanks for reading till end…..
Give your valuable feedback in comment.








