Android Room, Live Data And View Model


In this blog, we will learn about using Android Room, LiveData And View Model from the Android Architecture Components.

Why do I need to know about these?

Well, this makes Android development easy and fast.

What exactly is Android Architecture Components?

A new collection of libraries that help you design robust, testable, and maintainable apps. —

How does this help me as a developer?

It helps us address two pain points:

  1. Manage our UI components lifecycle
  2. Persist data over configuration changes

Ok, wanna read more, let’s get started :

We will cover three major parts of the Android Architecture Components Library.

  1. Room
  2. ViewModel
  3. LiveData


Android Room is a library that helps you in easily accessing the database. You can define your database by adding annotations in your Model class. Now you can query your data without having to deal with cursors or loaders.

If you have no knowledge about the android room, then you should read this article:

Android Room Persistence Library


If you have some knowledge about the android room and are having some difficulties still in using, you should read this article :

Saving Complex Objects using Android Room Library


View Model

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations. —

What this means is that ViewModel class can retain its state/data even during an orientation change.

ViewModels do not contain code related to the UI. This helps in the decoupling of our app components.

In Room, the database instance should ideally be contained in a ViewModel rather than on the Activity/Fragment.

Creating an Android ViewModel

Every ViewModel class must extend the ViewModel class. If your ViewModel needs the application context, it must extend AndroidViewModel. The ViewModel will contain all the data needed for our Activity.

Using this ViewModel and observing changes in activity/fragment.

All you need to do is activate your ViewModel Instance after your activity/fragment has been created. You can do this for activity by initializing the View Model Instance in onCreate Method of your activity or onActivityCreatedmethod of your fragment.

For Fragment something like this needs to be done :

For Activity something like this :

Whenever we need to use ViewModels inside our Activity, the Activity must extend LifecycleActivity.


LiveData is an observable data holder class. Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services. This awareness ensures LiveData only updates app component observers that are in an active lifecycle state.  —

What do we need to change for using live data in our current Android Project?

I have used only  these :

You can find the whole list of dependencies over here :

Changes in your Dao class.

Hope This helps you, Keep coding and keep sharing 🙂



Reference :

. . .
Discuss on Helpdesk

Leave a Comment

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


  • webserveis
    Nooo need to close Database?
    • Anchit Makkar
      Well, thanks for such a nice question,

      I personally never felt the need to close the database.
      As the room instance will automatically manage the same.

      As per the official docs on

      “You should follow the singleton design pattern when instantiating an AppDatabase object, as each RoomDatabase instance is fairly expensive, and you rarely need access to multiple instances.”

      So, i don’t think you should open and close the database.

      But if you have any such requirement then you can have a look at the close function.

      Do keep in mind that you will need to open the database again before actually performing any other operation.
      Tip : don’t close in onDestroy(), close in on Stop() and open in onStart().

      For any other discussion, feel free to communicate.

  • Hendra Wijaya Djiono
    Thank you for the great, but simple tutorial anchit. I just want to correct some part of your code.
    First, in your activity’s observer callback there is wrong type MyViewModel should be MyObject:
    public void onChanged(@Nullable List currentList)…
    should be
    public void onChanged(@Nullable List currentList)…
    Second, from your gradle dependency, you are also including kapt, but since your code is in Java, I think you don’t need to include it. But event if you are coding in Kotling, you can safely use kapt only instead of including both
    • anchit (Moderator)
      Yes, you are correct. Thanks for your keen observations.
      For point 1, i have updated.
      For point2, actually i was working on a mixture of java as well as kotlin, so i included both and added both over here.
  • css.php
    Start a Project

      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home