

Create a LiveData instance in your ViewModel class to hold the data.In order to use LiveData in your Android project, follow these steps: Consequently, we can use the Interface to communicate this response to the Main thread for updating the UI. 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. Let’s take the case of an application that’s developed in an MVP pattern. It de-couples tight integration between data, mediator, and the UI layers.

This is a core feature of an MVVM model where callbacks are sent from ViewModel to activity/fragment.


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. Let's assume that the user clicked the button twice, upon which the value of TextView is incremented from 0 to 2. As a result, you need not be concerned about the app component's lifecycle while updating the data.įor example, there's a Button and a TextView, the values of which will be updated based on the button click. Contrarily, if the app components are inactive, the changes will not be applied. 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. 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. Updating UI is one of the most used tasks in Android applications, and so LiveData plays a crucial role in MVVM architecture pattern. 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. 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. Such changes to the UI make application interaction attractive to a wider audience profile and leaves customers satisfied with a brand’s experience.įor a long time, Android developers struggled to easily implement such interactive components into the app’s UI due to architectural restraints. Application users expect UI components to respond interactively with the changes and updates in related data for a more interactive and dynamic usage experience.
