Android LiveData With Examples

In this tutorial, we’ll be discussing the LiveData architectural component in our Android Application.

For a better understanding of this tutorial, do take a quick detour to Android ViewModel.

Android LiveData

LiveData is a part of the architecture patterns. It’s basically a data holder that contains primitive/collection types. It’s used for observing changes in the view and updating the view when it is ACTIVE. Thus, LiveData is lifecycle aware.

We know that ViewModels are used to communicate the data to the View. Using ViewModels alone can be a tedious and costly operation since we need to make multiple calls each time the data has to alter the View. Plus we need to store the data Model at different places.

LiveData is based on the Observer Pattern and makes the communication between the ViewModel and View easy.

It observes for data changes and updates the data automatically instead of us doing multiple calls in adding and deleting data references from multiple places (for example SQLite, ArrayList, ViewModel).

Android LiveData vs RxJava

Android LiveData is somewhat similar to RxJava except that LiveData is lifecycle aware.

It won’t update your data in the view if the View is in the background. This helps us in avoiding exceptions like IllegalStateException etc.

How does our LiveData in the ViewModel update the Activity?
When we register the Observer in our Activity, we need to override the method onChanged(). The method onChanged() would get trigger whenever the LiveData is changed. Thus in the onChanged(), we can update the changed LiveData onto the View.

LiveData is just a data type which notifies it’s observer whenever the data is changed. LiveData is like a data changed notifier.

LiveData notifies the observer using setValue() and postValue().

setValue() runs on the main thread.

postValue() runs on the background thread.

Invoking getValue() on the LiveData type instance would return you the current data.

MutableLiveData

MutableLiveData is just a class that extends the LiveData type class.

MutableLiveData is commonly used since it provides the postValue(), setValue() methods publicly, something that LiveData class doesn’t provide.

LiveData/MutableLiveData is commonly used in updating data in a RecyclerView from a collection type(List, ArrayList etc).

In the following section, we’ll create an application that adds/deletes rows in a RecyclerView from the SQLite Database. We’ll use MutableLiveData that updates the RecyclerView records whenever the LiveData changes.

We’ll use DiffUtil to update the minimum number of RecyclerView row by comparing the old and new ArrayList.

Android LiveData Example Project Structure

android-livedata-project-structure

Add the following in your build.gradle file:

Android LiveData Code

The code for the activity_main.xml layout is given below:

The code for the list_item_row.xml layout is given below:

The code for DbSettings.java class is given below:

The code for FavouritesDbHelper.java class is given below:

The code for Favourites.java model class is given below:

So in our SQLite database we create a Table with three records : ID, URL, DATE.

The code for FavouritesViewModel.java class is given below:

MutableLiveData holds a List of Favourite instance objects. In the addFav() and removeFav() we notify data changes to the Observer that’s defined in MainActivity.

We create a copy of the ArrayList in order to compare the old and new one.

The code for MainActivity.java class is given below:

In the above code, we’ve defined the ReyclerView Adapter class in the Activity itself.

mFavViewModel.getFavs().observe(this, favsObserver); is used to set an Observer in the MainActivity that’ll be notified from the ViewModel class whenever the LiveData is updated.

The favsObserver anonymous class consists of the onChanged() method which provides the latest data which is then updated int the RecyclerView.

The output of the above application in action is given below.

android livedata example app

This brings an end to Android LiveData tutorial. You can download the project from the link below.

By admin

Leave a Reply

%d bloggers like this: