LiveData and LifeCycle Component in Android Architecture

In this tutorial, we will learn the new architecture component LiveData and LifeCycle in detail. In my last tutorial, we had familiar with new Dagger2 integration with android architecture component. I would be recommended to check this post that will help you to understand the New Dagger2 dependency injection concept. Here is the post detail New Dagger2 dependency injection integration with android architecture component. If you are curious to know the old Deagger2 implementation of MVP design pattern then please check this post.

LiveData is one of the newly introduced architecture components. LiveData is an observable data holder. This allows the components of your app to be able to observe LiveData objects for changes happened.  LiveData is a leverages reactive programming which helps us manipulate data easily. It shares some similarities with Stream in Java 8 and Observable or Flowable in RxJava or RxJava2. However, LiveData has an advantage is that it is lifecycle-aware specific for Android. So, it works closely with LifeCycle components.

LiveData is a variant of the original observer pattern, with the addition of active/inactive transitions. For example, we can say in a network connectivity, Live data is observing the connection between active and inactive state automatically and we can do the respective actions in this scope.  Let’s see by an example but before we need to add a relevant dependency in your build.gradle file.

Here is the network connectivity Live data example.

Here NetworkLiveData is ready, now we need to observe on network change. Live data is only delivered the change update if any observer is listening to this change else it will not. In this case, memory leak chance will be removed.

LifeCycle addresses the Android lifecycle problems ineffective and easy ways. It has two main parts. LifecycleOwner exposes its state changes or it is an interface for components having Lifecycle (such as Activities and Fragments)., and LifecycleObserver listens to these changes to make appropriate steps. LifecycleObserver subscribes to LifecycleOwner’s lifecycle state changes.

It does not have to rely on Activity or fragment’s onStart() or onStop()lifecycle callback to initialize and stop it. Here we can take an example of Location update.

And we can call this in Fragment.

Using LiveData provides the following advantages:

Ensures your UI matches your data state
LiveData follows the observer pattern. LiveData notifies Observer objects when the lifecycle state changes. You can consolidate your code to update the UI in these Observer objects. Instead of updating the UI every time the app data changes, your observer can update the UI every time there’s a change.

No memory leaks
Observers are bound to Lifecycle objects and clean up after themselves when their associated lifecycle is destroyed.
No crashes due to stopped activities, If the observer’s lifecycle is inactive, such as in the case of an activity in the back stack, then it doesn’t receive any LiveData events.

No more manual lifecycle handling
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.

Always up to date data
If a lifecycle becomes inactive, it receives the latest data upon becoming active again. For example, an activity that was in the background receives the latest data right after it returns to the foreground. Proper configuration changes If an activity or fragment is recreated due to a configuration change, like device rotation, it immediately receives the latest available data.

Wrapping up: LiveData is something like Rx Observable that reacts if any change happened in Object. We can use the LiveData in android application development in multiple places like Fetching data by using Retrofit or Database.  Here are the best offer and recommendation for you to download this. Here is the Github link for an Android Architecture example of Retrofit and Database LiveData with LifeCyle.

Please do subscribe email to get all newsletters of this blog and if you feel that this post will help you to better understand then do not forget to subscribe, share and comment below. Ok, then I will see you in my next tutorial till then enjoy your life and happy coding 🙂

I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.