How to use LiveData?

Exaud
Published on Feb 15, 2019 in Android Developers Resources
How to use LiveData?

As you probably already know, Android Architecture Components were introduced at Google I/O 17 and, since then, there has been a lot of discussion about them. If you want to catch up on the subject, you can start by watching the Introductory talk Google made at Google I/O.

We noticed that in a lot of tutorials and code samples, Architecture Components are tied together. Sometimes, it looks like they should all be used together for them to work. What if you already have a stable application and you want to start using Architecture Components? You probably don’t want to redo all of the code and risk a few regressions.

The recommended approach is to migrate to one component at a time. You don’t need Navigation to use Room and, you certainly don’t need Room to use LiveData. We did talk about Room in our blog a few months ago, so now, we are going to introduce you to LiveData.

LiveData is an observable data holder class. But unlike a regular observable, it has a major advantage, it is lifecycle-aware. That means that only the observers that are in an active lifecycle state are updated. You can read a little bit more details about LiveData in the official source.

Now, let’s learn by example. Let's imagine we have all of the book titles we read stored in a database and we want to read one of them again, but we don’t know which one to pick. We will press a button and choose one of the titles in our collection randomly and display it in the UI. In the example, we will be using a subclass of LiveData called MutableLiveData. It has setValue and postValue properties used to notify the UI whenever the data changes. We will also be using MVVM.

In your ViewModel, you will need the MutableLiveData object, the method that returns that object and a method that calls setValue on it:

How to use LiveData?

To be able to observe changes of this LiveData object, we can write the following lambda in our Activity/ Fragment:

How to use LiveData?

The observers will always be notified when the method setValue() is called on the MutableLiveData object. In this scenario, when the method loadBook() is invoked, we will receive that update in the View and display the book title in a toast. This approach can be followed to do whatever we need to meet our client requirements.

Find more top mobile app developers worldwide on AppFutura.

Looking for an app or software development company?

You can post a project on AppFutura for free and explain your needs for app or software development. You will receive quotes from qualified companies and will be able to hire the best candidate through a safe payment system.

Post a project

About the author
Exaud
Exaud
ContactVisit website

Exaud is a top Portuguese development studio focusing on IoT, mobile solutions and embedded systems. Exaud started as a successful technological partner for major automobile brands and expanded their operations to the mobile world and IoT development. Their expertise in integrating hardware and software gives them an edge in the new technology trends.

You might also like