In a previous post I mentioned LiveData and I said I will talk about it in the future, I think now is a good time to talk about LiveData because in my Last (post) I talked about ViewModel, and I gave an example of how to use ViewModel to provide and maintain your data for your UI components (aka Activity or Fragment). Both ViewModel and LiveData are parts of the Android architecture components. According to the documentation “Android architecture components are a collection of libraries that help you design robust, testable, and maintainable apps. Start with classes for managing your UI component lifecycle and handling data persistence.”
So What is LiveData?
Again according to the documentation “LiveData is a data holder class that can be observed within a given lifecycle”. This means you set up an observer in such as Activity of Fragment and that observer will be notified if its wrapped data changed, instead of requesting the data each time from the ViewModel; but that notification happens only in Activity or Fragment active state(Started or Resumed). LiveData is considered a wrapper that can be used with any data including objects that implements collections such as List. A LiveData object usually lives in the ViewModel class.
In this post, I will give you an example of how we can use LiveDate. We will build a simple application containing one text view and one button when the button is pressed a text will appear in the text view. This example is based on the famous game “Werewolf”. This example as the last post contains a ViewModel and also the Live Data is defined in the View Model itself. So let’s start coding.
I used the following dependencies:
And now sync the project.
activity_main.xml is as follows:
There are some drawables and strings that I used in the actvity_main.xml, you will find it in the complete project in Github.
I called the ViewModel RoleViewModel, the RoleViewModel class is as follows:
I used the Random class, so each time you use getRole() function it will return a random string from the list.
The MutableLiveData class has setValue() (synchronously) and postValue() (asynchronously) methods, these methods are public and can be used to change the value stored in a LiveData object.
The MainActivty is as follow:
When we press the Show Role button, the data is changed and post its new value to LiveDate using postValue().
In most cases, setup observes in onCreate() method is better, so there will be no redundant calls from onResume() method and to ensure that as soon as the activity or fragment becomes active they will have data to be displayed.
The output of the app will be as follows:
This is a simple example of how to use LiveData and you can know more about it in Android Developers website. And you can find the project in Github. So that’s for this week. If you have any thoughts or questions please share them with us in comments.
Till next week.