Live Data + Kotlin (Android Architecture Component)

Nikhil Singh
4 min readOct 23, 2020
Live Data (Android Architecture Component)

Hi Friends,

Let's start with the Live Data !!

LiveData is an observable data holder class. Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services. This awareness ensures LiveData only updates app component observers that are in an active lifecycle state.

LiveData considers an observer, which is represented by the Observer class, to be in an active state if its lifecycle is in the STARTED or RESUMED state. LiveData only notifies active observers about updates. Inactive observers registered to watch LiveData objects aren’t notified about changes.

Observer:

An Observer is an interface that is implemented by a class or any android component e.g. Activity, Fragment, Service, etc.

A class can implement the Observer interface when it wants to be informed of changes in observable objects.

This Observer interface has a method onChanged(T t). A simple callback that can receive from LiveData. here t of type T shows the new data when this onChanged called. So, This T may be of any kind of object e.g. String, Collections, User-defined, etc.

The advantages of using LiveData:

  • Ensures your UI matches your data state
  • No memory leaks
  • No crashes due to stopped activities
  • No more manual lifecycle handling
  • Always up to date data
  • Proper configuration changes

Difference between LiveData and MutableLieveData:

  • LiveData is immutable while MutableLiveData is mutable.
  • MutableLiveData extends LiveData and provides methods like setValue() and postValue().
  • Use LiveData when you don’t want to modify it because the methods like setValue() & postValue() are not public.

Difference between setValue() and postValue():

  • setValue(): If there are active observers, the value will be dispatched to them. This method must be called from the main thread.
  • postValue(): Posts a task to the main thread to set the given value. If you called this method multiple times before the main thread executed a posted task, only the last value would be dispatched.

Conclusion:

setValue() method must be called from the main thread. But if you need to set a value from a background thread, postValue() should be used.

Before start work with the Live Data, Lets’ understand the few steps to work with.

  1. Create an instance of LiveData to hold a certain type of data. This is usually done within your ViewModel class.
  2. Create an Observer object that defines the onChanged() method, which controls what happens when the LiveData object’s held data changes. You usually create an Observer object in a UI controller, such as an activity or fragment.
  3. Attach the Observer object to the LiveData object using the observe() method. The observe() method takes a LifecycleOwner object. This subscribes the Observer object to the LiveData object so that it is notified of changes. You usually attach the Observer object in a UI controller, such as an activity or fragment.

Note:

You can register an observer without an associated LifecycleOwner object using the observeForever(Observer) method. In this case, the observer is considered to be always active and is therefore always notified about modifications. You can remove these observers calling the removeObserver(Observer) method.

When you update the value stored in the LiveData object, it triggers all registered observers as long as the attached LifecycleOwner is in the active state.

LiveData allows UI controller observers to subscribe to updates. When the data held by the LiveData object changes, the UI automatically updates in response.

Here we have scratched all the necessary things about LiveData, So Let’s implement it in our project.

implement LiveData and View Modle library in build.gradle(app-level)

Don’t forget to use the kapt plugin at the top.

apply plugin: 'kotlin-kapt'

So guys in our LiveData demo application I am gonna cover the following.

  • Basic LiveData with CountDownTimer:

Above we have the BasicViewModel class in which I am just using the CountDownTimer that starts from 10 sec(10000 ms) and on every OnTick, I set the value of sec to _seconds MutableLiveData object and later return it using a public LiveData type seconds method.

In the LiveDataBasicActivity, I just start the timer using the start button that we have in UI and let the value change by LiveData automatically. Every time the value of seconds change and update the textView because our ViewModel instance seconds observe the value of _seconds updating in the BasicViewModel by CountDownTimer.

You can find the complete code at my GitHub link here.

Thank You !!

Nikhil Singh (Android Developer)

--

--