Mobile application users expect UI components to reply interactively with the changes and updates in related data for a more interactive and dynamic usage experience. Such changes to the Application UI make application interaction attractive to a user and result in good user experience. For a protracted time, Android developers struggled to simply implement such interactive components into the app’s UI because of architectural restraints. Updating Application UI is one of the most important tasks in Android applications environment, then Live Data plays a very important role in MVVM architecture pattern for development.

Update the UI of the app due to change in some data is the most important tasks in Android. For example, when you like some post on Facebook, then the like count will be increased when some like. So we are changing the UI based on some data updation and there are many more examples of these kinds of UI change. So, these changes in the views(Activity/fragment) of your app can be done out with the help of ViewModel and to make it easy the working of a ViewModel, Google have introduced the concept of LiveData. which is a part of Android Jetpack.

What is LiveData?

LiveData is an lifecycle aware and observable data holder class. means it take care of the lifecycle of app components, such as activities, fragments, or services.

In some other words, LiveData is simply an observable data holder which is used to observe the particular view (Activity?fragment) changes  and then update that change. It is lifecycle-aware component that is whenever a data is update or changed then it will send update to only those app components which are in its active state. If the component of app is not active state at current time and becomes active in the future at other time, then it will send the updated data to that app component. So, you do not worry about the lifecycle of the app component when your are  updating data.

For example, if you are updating some TextView with some value without the use of LiveData and , then on the rotation of the screen, you have to set again the new value in the TextView because the Activity will be recreated means onCreate() methods will run on screen rotation. But when you are using LiveData, you just need to do update the value and that’s it. and you do not need to care about the lifecycle.

Why do we need LiveData?

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

How to work with LiveData

Here is 4 steps to use LiveData in your project

1:-First, create a LiveData instance in your ViewModel for holding the data.

private MutableLiveData<String> yourname;


2:-After that you have to set the data in the LiveData by using such type of methods setValue and postValue.

viewModel.name.setValue(/your updated data /);
or
viewModel.name.postValue(/your updated data /);

3:-After that, to observe live data by the observer like Activity or Fragments  you have to return the LiveData..

public LiveData<String> getYourname(){
     return yourname; 
}

4:-And finally, you need to observe these data in your views with the help of observer. In the observer, you have to define all the changes in the UI that you want to perform on data change.

getYourname().observe(this, yourname -> {
     // UI updation
}

Type of LiveData

LiveData is an Abstract Class. So we can’t be used as itself. So Google has implemented some simple concrete class we could make use of.

MutableLiveData

This is the most simple LiveData, Which when get updated and notify items observer. it extends LiveData internally and also have the two methods of LiveData which is publicly available.

  • setValue() :it will set the value and will dispatch the value to all the active observers.

One thing that you have to keep in mind about setvalue() is that it can only done in main thread only it can not be done in background thread.

  • postValue() : it is use to  post a task to main thread by which it can override value set by setvalue.

As we know that setvalue cannot be called from background thread so postvalue must be used to set value from background thread.

MediatorLiveData

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

LiveData will trigger when configuration change or screen rotation occur. But there are some case in which we go with SingleLiveEvent because we need to perform UI updates by clicking on a particular View to perform validations or show a progress bar during server call.

SingleLiveEvent is a subclass of MutableLiveData. It is take care of the View’s lifecycle and can observe or tack care the data with a single Observer.

Advantages of using LiveData

LiveData provides us with the subsequent advantages:

UI matches with the data state

LiveData could be a lifecycle aware. When app is in active state and whenever a component’s data gets updated or changed, only the actual component UI matches the state without fail. we do not must update the UI when when app data changes because the Observer takes care of that.

Increased stability of code

The code stability increases throughout the applying component lifecycle:

* There are not any crashes when activities are stopped. If the app components are inactive those changes aren’t affected. So, you would like not worry about the app component lifecycle while updating the info. And when an activity, it doesn’t receive any LiveData events.

* Memory leaks are reduced because unwanted events/allocations are automatically handled

* No concerns while unsubscribing any Observers

* Changes needn’t be updated within the latest data during a configuration/screen orientation change

No manual lifecycle handling

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

Always up to date with the latest data

Whenever there’s a change within the data stored in LiveData, all the observers related to the LiveData are going to be notified (only when the app components are live). If any app component is not in active state, LiveData concept won’t work. But the most recent data are going to be observed by the Observer. When the component is reactivated, the foremost recent data are applied thereto.

Proper configuration changes/screen orientation

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

You may also like...

0 Comments

No Comment.