LiveData

It’s very common that your activity or fragment observe some data, and whenever that data changes, It wants to refresh itself.It’s like your activity receives a callback, but the activity already stopped.If an activity or a fragment is stopped there is no reason to update that UI.If the activity happens to become visible again then you want to do it.

We want to share resources across multiple fragments or activities.For example, the Location of the device is the same from fragment to fragment.If you have two fragments, why do you need to create two listeners to listen to the same location?

Hence, Android create this new LiveData clause.

LiveData

LiveData is a data holder, it just holds some data.It’s like an observable but the tricky thing about it is LifeCycle aware.If you are observing a LiveData, you don’t need to unsubscribe, the right things will happen at the right times.

  • It is a Lifecycle aware Observable.
  • It is very simple to start and stop semantic.(Doesn’t matter how many observers you have or what state they are we merge all of it into one lifecycle).
  • It doesn’t have any activity or fragments inside it works with both of them. It is also really used to test LiveData because it’s’ kind os android free.
  • Android guarantee that your observable will never, ever be called in a state where you cannot run a FragmentTransaction.
  • This is very very specifically designed to work well with your Activity and Fragments.

 

How LiveData Work?

So onCreate, it called to observe, it said LiveData is observable.And as soon as the activity starts it starts receiving data changes. So whenever the LiveData value changes, android displace that event back to your observer inside the activity.

LiveData Rotate screen

On Activity Rotated.So you know that the activity will be stopped.and what happens at the same time, the LiveData happens to be updated.If that happens LiveData is not going to tell the activity about this change. Similarly, if the activity is destroyed, Android will automatically remove the subscription because that activity is gone.

Now, The activity was rotating, so you know that android is going to recreate that activity.and then we are observing the same LiveData back. As soon as the activity starts, it’s going to receive last available data.So your UI is going to have the Data before it gets a chance to draw.

LiveData onStop

If the user hits the Home button which means the activity will be stopped. Again if the LiveData changes while the activity is stopped, is not going to receive any events. Even if the data changes we are not going to tell it.

LiveData reopen app

But as soon as if the user comes back to the application, android will give it the last available data.

So this is why we call LiveData is not just a stream of events.It holds on to data so that if any observer comes, it receive the last available value. and the eventually, the user backs out of that activity, and then we remove that subscription.

LiveData Example

Architecture Components are available from Google’s Maven repository. To use them, follow these steps:

Add the Google Maven repository

To add it to your project, open the build.gradle file for your project and add the highlighted line as shown below:

Adding Components to your Project

The activity updates the UI when the new number generated.

LiveData Example

 

LifecycleOwner

MainActivity is an instance of LifecycleActivity, which can provide the state of a lifecycle. This is the class declaration:

To expose data as an observable, wrap the type in a LiveData class.LiveData is a special observable class which is lifecycle-aware, and only notifies active observers.

Add the following code to the MainActivity class, to create the subscription:

Sets the value. If there are active observers, the value will be dispatched to them.

This method must be called from the main thread. If you need set a value from a background thread, you can use postValue(Object).

RxJava vs LiveData

Key different between RxJava and LiveData.LiveData is a holder and not a stream.So we have a reference to the last value, and observer immediately receives the last value when they start to observe liveData.

And now the big different is a threading model.As you know Rxjava has a very sophisticated threading model.It’s extremely powerful but in most cases, you probably don’t need it.LiveData has everything on the main thread.

LiveData is Lifecycle aware.It knows about android life cycles, and when you want to observe a LiveData, you can pass in this LifeCycle so that it manage your subscription.The nice thing about LiveData that is that your observer, and that’s all you do. You don’t need to write onStart, onStop.

You initialize thing as more like find and forget.You initialize and you’re done.

It’s mush easier.As many of you may know the learning curve of Rxjava is super steep.It is simpler, it ‘s faster it’s lightweight, It’s well integrated with a framework.If you feel like you love reacting programming a lot, You want to bring it not only to the relation between UI and the state. You want to bring it to the business part of your application.Then you may consider the addition of RxJava, because it gives you more power.Android will actually help you to do that.Android has this extension to the library, Which gives a possibility to create LiveData from Publisher and create Publisher from LiveData.So this Integration should be quite smooth.

What is an active observer?

You can also extend the LiveData class.Because LiveData provides two really handy callbacks.

We define an active observer as an observer that’s in the STARTED or RESUMED state which is like an activity user is currently seeing.So if you have an observer in the back stack, there’s no reason to put this inactive.There’s no reason to update that activity because the user will never, ever see what’s going on there.

So inside our connect method all we need to do is whenever the system Location Manager sends us a new location we call setValue on ourselves.Then the LiveData knows which are the activity observers, and delivers the data to those observers.if one of the Observer was on the back stack and then becomes visible again, LiveData takes care of sending the latest data back to that observer.

onInactive(), Which means you don’t have any observers, So don’t bother changing your value if it is something that you care about.

 

Related Post

Lifecycle Aware Components

ViewModel

Room Persistence Library

Fragment Tricks

ConstraintLayout 1.1.x

Leave a Reply

Your email address will not be published. Required fields are marked *