VOOZH about

URL: https://www.geeksforgeeks.org/android/livedata-in-android-architecture-components/

⇱ LiveData in Android Architecture Components - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

LiveData in Android Architecture Components

Last Updated : 23 Jul, 2025

LiveData is one of the android architecture components. LiveData is an observable data holder class. What is the meaning of observable here the observable means live data can be observed by other components like activity and fragments (Ui Controller). The most important thing about LiveData is it has the knowledge about the Life cycle of its observers like activity or fragment. That means Live data only updates the app components like Activity or Fragments which are in active life cycle state. LiveData notifies the observer(Activity or Fragment) which are in Started or Resumed life cycle state. Inactive observers registered to watch LiveData objects aren't notified about changes. Here inactive observers mean which are not in the state of Started or Resumed. One can register an observer paired with an object that implements the LifecycleOwner interface which we will see in our example. This relationship allows the observer to be removed when the state of the corresponding Lifecycle object changes to DESTROYED.

This component is an observable data holder class i.e, the contained value can be observed. LiveData is a lifecycle-aware component and thus it performs its functions according to the lifecycle state of other application components. Further, if the observer’s lifecycle state is active i.e., either STARTED or RESUMED, only then LiveData updates the app component. LiveData always checks the observer’s state before making any update to ensure that the observer must be active to receive it. If the observer’s lifecycle state is destroyed, LiveData is capable to remove it, and thus it avoids memory leaks. It makes the task of data synchronization easier.

👁 LiveData


It is necessary to implement onActive and onInactive methods by LiveData:


In order to observe a LiveData Component observer(LifecycleOwner, Observer<T>) method is called:


Implementation in Android App

In this example we will just create a simple counter app, that just counts 5 seconds, you can do anything using LiveData but for now let's build this small app. 

Step 1: Add these dependencies in your build.gradle file

dependencies {
...
implementation ("androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.7")
implementation ("androidx.lifecycle:lifecycle-livedata-ktx:2.8.7")
implementation ("androidx.lifecycle:lifecycle-runtime-ktx:2.8.7")
implementation ("androidx.core:core-ktx:1.15.0")
}

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.

activity_main.xml:


Step 3: Create a ViewModel class

Create a Kotlin class file MainViewModel.kt. Our MainActivity class file extends the ViewModel class.

MainViewModel.kt:

Note: Here we are using MutableLiveData right. but the question is why? because there is already LiveData is available, MutableLiveData extends LiveData class and two functions setValue() and postValue() are publicly available for use.


Step 4: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file.

MainActivity.kt:

Note: Here inside Observe() "this" is the Life cycle owner as we discussed above to observe the value, we should pass the Lifecycle Owner. Here this means MainActivity which is the observer here.

Output: 

Advantages of Using LiveData

  • : LiveData follows the observer pattern. LiveData notifies Observer objects when there is any change occurs.
  • : Observers are bound to Lifecycle objects and clean up after themselves when their associated lifecycle is destroyed.
  • : UI components just observe relevant data and don’t stop or resume observation. LiveData automatically manages all of this since it’s aware of the relevant lifecycle status changes while observing.
  • : If an activity or fragment is recreated due to a configuration change, like device rotation, it immediately receives the latest available data.
Comment
Article Tags:

Explore