Welcome to Innominds Blog
Enjoy our insights and engage with us!

Introduction to LiveData in Android

By Manimala Gudi,

MicrosoftTeams-image (8)-1

Application users expect UI components to respond interactively with the changes and updates in related data for a more interactive and dynamic usage experience. Such changes to the UI make application interaction attractive to a wider audience profile and leaves customers satisfied with a brand’s experience.

For a long time, Android developers struggled to easily implement such interactive components into the app’s UI due to architectural restraints. But when Android Jetpack introduced LiveData as part of architecture component, developers embraced it in order to escape the clutter of callbacks, coupled classes, and crashed activities. The Android Jetpack is a suite of libraries that assist developers to follow best practices by providing backwards compatibility, decreased boilerplate code and code that works consistently across various devices and Android versions.

Updating UI is one of the most used tasks in Android applications, and so LiveData plays a crucial role in MVVM architecture pattern.

What is LiveData?

Unlike a regular observable, LiveData is an observable data holder class that is used to observe the changes of a ViewModel and update those changes. LiveData is lifecycle-aware, which means that whenever data is updated or changed, the changes are only applied to the specific app components that are in an active state. Contrarily, if the app components are inactive, the changes will not be applied. As a result, you need not be concerned about the app component's lifecycle while updating the data.

Introduction to LiveData in Android

For example, there's a Button and a TextView, the values of which will be updated based on the button click. Let's assume that the user clicked the button twice, upon which the value of TextView is incremented from 0 to 2. Now when we rotate the device to change the layout configuration from Landscape to Portrait orientation, with LiveData in use, the Textview value will be retained at 2; and when it is not, the value of the TextView will be reset to 0.

Why do we need LiveData?

LiveData works well in resolving mainly two issues:

  • It removes the leaks caused by the interfaces/callbacks that send results to the UI thread. This is a core feature of an MVVM model where callbacks are sent from ViewModel to activity/fragment.
  • It de-couples tight integration between data, mediator, and the UI layers.

Let’s take the case of an application that’s developed in an MVP pattern. If we want to update the UI based on a network call response, we can call a method from the View layer to the Presentation layer to perform a network request and write the AsyncTask to do the work. Consequently, we can use the Interface to communicate this response to the Main thread for updating the UI.

LiveData-image 2

Using LiveData

In order to use LiveData in your Android project, follow these steps:

  1. Create a LiveData instance in your ViewModel class to hold the data.
  2. Set the data in LiveData using setValue().
  3. Return the LiveData using postValue(). Only this data will be observable by the Observer which is in Views i.e., activity or fragment.
  4. Finally, observe the data with the help of the Observer() function. Inside Observer(), you can define all your necessary changes in the UI that you want to perform when there is a change or update in the data.

Once you have completed the above steps, any change in the data stored in LiveData will cause all the Observers associated with the LiveData to be notified. This applies only when the app components are live. Even if the app components were not in the active state, they will be notified once they become active.

LiveData-image 3

Types in LiveData

There are subclasses in LiveData that are useful for their properties when updating the UI.

  1. LiveData
  2. MutableLiveData
  3. MediatorLiveData

LiveData is immutable by default. By using LiveData we can only observe the data and cannot set the data.

MutableLiveData is mutable and is a subclass of LiveData. In MutableLiveData we can observe and set the values using postValue() and setValue() methods (the former being thread-safe) so that we can dispatch values to any live or active observers.

MediatorLiveData can observe other LiveData objects such as sources and react to their onChange() events. MediatorLiveData will give us control over when we want to perform an action in particular or when we want to propagate an event.

LiveData will trigger upon configuration change or screen rotation. But in some scenarios, like when we need to perform UI updates by clicking on a particular View to perform validations or show a progress bar during server call, we go with SingleLiveEvent.

SingleLiveEvent is a subclass of MutableLiveData. It is aware of the View's lifecycle and can observe the data with a single Observer.

live-data-4

 

Advantages of using LiveData

Using LiveData provides us with the following advantages:

UI matches with the data state

LiveData follows the Observer pattern. In an active state, whenever a component’s data gets changed or updated, the particular component UI matches the state without fail. We don't need to update the UI every time the app data changes because the Observer takes care of that.

Increased stability of code

The code stability increases throughout the application component lifecycle:

  • There are no crashes when activities are stopped. If the app components are inactive those changes are not affected. So, you need not worry about the app component lifecycle while updating the data. And in the case of an activity in the back stack, it doesn’t receive any LiveData events.
  • Memory leaks will be reduced because unwanted events/allocations are automatically handled
  • No concerns while unsubscribing any Observers
  • Changes need not be updated in the latest data during a configuration/screen orientation change

No manual lifecycle handling

LiveData is aware of the View's lifecycle. Therefore, the UI components just observe relevant data and don’t stop or resume observation. LiveData automatically manages all of these changes while observing.

Always up to date with the latest data

Whenever there is a change in the data stored in LiveData, all the observers associated with the LiveData will be notified (only when the app components are live). If any app component is in an inactive state, LiveData concept will not work. But the latest data will be observed by the Observer. When the component is reactivated, the most recent data will be applied to it.

Proper configuration changes/screen orientation

If an activity or fragment is recreated due to a configuration change or screen orientation, it immediately receives the latest available data and updates the UI.

Sharing resources

Like the Singleton pattern, we can extend our LiveData object too to wrap system services so that they can be shared in our app. Once the LiveData object connects to the system service, any observer that needs the resource can watch the LiveData object easily.

More on LiveData: Example

Consider an app with two screens - Login and Home.

  1. Login Screen --- > covers the SingleLiveEvent concept
    1. Login screen has Email, Pwd edittexts, and login button
    2. For email and password validation we use SingleEventLive data
    3. After successful authentication, it will navigate to the Home Screen

      live-data-8
  2. Home Screen ---> covers MutableLiveData, MediatorLiveData concept
    1. The Home screen contains three buttons

    2. The background colour of the layout can be changed by clicking the first button.

      live-data-9

    3. When you click the second button, the background colour of the first and second buttons changes. Use MutableLiveData for such UI updates.

      live-data-10
    4. When you click the second button, the background colour of the first and second buttons changes. Use MutableLiveData for such UI updates.

      live-data-11

The background of the third button will change depending on whether the first or second button is hit. MediatorLiveData is used for this UI update.

Other relevant information to note about the application:

Architecture pattern: MVVM

Language: Kotlin

Gradle Dependency

live-data-5

View Models Code Snippets

live-data-6

live-data-7

 

Do not use LiveData when:

  1. You need to use plenty of operators on the information. Even though LiveData provides tools such as Transformations, there's solely the Map and switchMap to your aid.

  2. You don't have UI interactions with information.

  3. You have one-shot asynchronous operations.

  4. You don’t have to persist cached information into UI.

Conclusion

Incorporating LiveData into a project significantly reduces boilerplate code for UI updates in the application. The LiveData concept completely mitigates the major problems like orientation change issues, we experienced while updating the UI with regard to the interfaces concept. LiveData also ensures that the UI is always up to date even when the app’s configuration is changed.

Innominds specializes in application development by incorporating the latest digital trends across all devices and platforms. Check out our Mobility services for more information.

Topics: Mobility

Manimala Gudi

Manimala Gudi

Manimala Gudi - Engineer - Software Engineering

Subscribe to Email Updates

Authors

Show More

Recent Posts