Mallow's Blog

Android Architecture Component – VIEWMODEL

INTRODUCTION :

First we need to understand what is meant by android architecture components ? It is nothing but a collection of libraries that helps you in designing a robust, testable, and maintainable apps. It was released by google in google I/O 2017. Now in 1.0 stable version.

Following are the key components of Android Architecture and we are going to see about ViewModel in specific :

  • ViewModel
  • Room
  • LiveData
  • Paging
  • DataBinding
  • Lifecycles
  • Navigation and 
  • WorkerManager

VIEWModel :

One of the major headaches for android developer is that they need the fetch data from database (or) call API and present it UI components without delay. Another major problem is that to sustain data (or) fetch data again for orientation changes. For example, let’s assume you have some Activity. Probably you’ll also have some class to store and provide data for UI (like Presenter known from MVP or ViewModel from MVVM). What happens when you rotate the screen? your activity gets recreated probably you need to do fetch data from database (or) make API call again and pass data to the UI components again. There is also possibility of memory leaks of not handling the view holder properly as it may sustain more than the activity. For this google provided alternative solution using the architecture component – ViewModel.

ViewModel stores your data and it is lifecycle aware. ViewModel does the following for you :

  • ViewModel will be automatically created by factory methods so you don’t worry about creating or destroying it and as simply as its lifecycle will be handled.
  • Whenever screen gets rotated no need to worry about fetching data again it will be handled automatically and provides the data to the UI components.

ViewModel Vs onSaveInstanceState() :

Both ViewModel and onSaveInstanceState() are different and both can be used in our application. ViewModel is used situations in where we need to store data for UI components and does not survive when app gets killed. onSaveInstanceState() survive even after app gets killed. So why can’t we use onSaveInstanceState() in all situations? It is not possible because onSaveInstanceState() cannot contain more data and needs to be Parcelable to store and retrieve data. Both satisfy their own purposes.

Sample Implementation of ViewModel :

Let’s understand the ViewModel with sample implementation as below :

  1. First need to add the dependency :
implementation "android.arch.lifecycle:extensions:1.0.0"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0"
  1. Create a sample UserViewModel, it must be extending the ViewModel class
public class UsersViewModel extends ViewModel {

     private List<User> userList;
 
     public List<User> getUserList() {
        if (userList == null) {
             usersList = loadUsers();
         }
         return userList;
     }
 
     private List<User> loadUsers() {
         // do something to load users
     }
 }
  1. Now we are able to get our userViewModel class in our activity
public class UsersActivity extends AppCompatActivity { 

     @Override
     protected void onCreate(final Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         UsersViewModel usersViewModel =
             ViewModelProviders.of(this).get(UsersViewModel.class);
 
         showUsers(usersViewModel.getUserList());
     }
 }

That’s it so simple to use ViewModel and it saves lots of our time.

CONCLUSION : That’s it so simple to use ViewModel and it saves lots of our time. Already we have seen about Room a component of android architecture. Hope we will see more important architecture components in the upcoming blogs.


Bringe Raj S B,
Android Development Team,
Mallow Technologies.

Leave a Comment

Your email address will not be published. Required fields are marked *