Using ViewModel with RoomDatabase | Android Jetpack

Ashish Rawat
MindOrks
Published in
4 min readMar 15, 2019

In this article, I am going to talk about ViewModel.

Those who do not know what is Room database and Room database or has not worked on it, I have linked my previous blog below

ViewModel is part of the Android Jetpack that is used to handle the lifecycle configurations.

Here is the video from Android Developers Channel:

Introduction

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way.

The ViewModelclass allows data to survive configuration changes such as screen rotations.

A ViewModel is created when an Activity or Fragment is created and will be retained until Activity or Fragment is finished.

If the UI destroys or re-creates, any transient UI-related data you store in them is lost. but if you use ViewModel then it will not be destroyed if its owner is destroyed for a configuration change (e.g. rotation) and the new instance of the owner will just re-connected to the existing ViewModel.

https://developer.android.com/topic/libraries/architecture/viewmodel#lifecycle

ViewModel created with activity and destroyed when activity finishes.

onCreate may be called several times during the life of an Activity, such as when the app is rotated, but the ViewModel survives throughout.

Why use ViewModel

The purpose of the ViewModel is to acquire and keep the information that is necessary for an Activity or a Fragment. The Activity or the Fragment should be able to observe changes in the ViewModel. ViewModels usually expose this information via LiveData or Android Data Binding. You can also use any observability construct from your favorite framework.

ViewModel vs onSaveInstanceState

  • onSaveInstanceState() is designed to save a small amount of transient data, but not complex lists of objects or media data.
  • A ViewModel can delegate the loading of complex data and also act as temporary storage once this data is loaded.

ViewModel with LiveData

ViewModel works with Room and LiveData to replace the loader. The ViewModel ensures that the data survives a device configuration change. Room informs your LiveData when the database changes, and the LiveData, in turn, updates your UI with the revised data.

Using ViewModel

Step 1: Create a ViewModel class

you’ll make a ViewModel class for each screen in your app

If you need to use context inside your ViewModel you should use AndroidViewModel because it contains the application context (to retrieve the context call getApplication() ), otherwise, use regular ViewModel.

AndroidViewModel vs ViewModel

A ViewModel on its own has no good way to get a Context.

AndroidViewModel supplies an Application for use as a Context, and specifically supplies the Application singleton so we are sure that the Context itself does not represent a memory leak.

In our case, we are using Room and we need context so we will use AndroidViewModel

Step 2: Add ViewModel to Activity

when the ViewModelProviders.of method is called for the first time by Activity, it creates a new ViewModel instance. Whenever onCreate is called, it will return the pre-existing ViewModel associated with the Activity. This is what preserves the data.

In our case, we are retrieving the LiveData object from the ViewModel to set an observer.

We have also linked a repo if you need to see the example.

That’s all for ViewModel.

This is a series for Android Jetpack, we will be back with other Android Jetpack Components.

Thanks for reading this article ❤

If we got something wrong? Let me know in the comments. we would love to improve.

Clap 👏 If this article helps you.

Connect with me on LinkedIn.

Check my GitHub repositories.

Follow me on Twitter.

--

--