Skip to main content

Android Livedata and MutableLiveData



Open Event Android  
Android Livedata and
MutableLiveData
December 14, 2018

Problem

Suppose that we need to perform actions in light of changes in lifecycle status of Android
Component. Some of the time you may wanna observe certain qualities being changed
over the setup change. We all faced these problems somewhere down the road, trying a
lot of different patterns MVP, MVVM, and others. But to implement these patterns is also
a big problem for beginners.

Solution

The good news is that with the Android Architecture Component and more precisely with the
lifecycle LiveData and ViewModel, we can easily overcome these problems.
LiveData
LiveData is an observable data holder. It is also a lifecycle aware. By lifecycle aware I mean,
it can only be observed in the context of a lifecycle, more precisely in the context of an
Activity or Fragment lifecycle. By passing the reference of an Activity or Fragment,
it can understand whether your UI onScreen, offScreen or Destroyed. After passing the
UI object to LiveData, whenever the data in the live data changes. It notifies the lifecycle
owner with updates and then the UI redraw itself with updates.
Advantages of LiveData

themselves when their associated life cycle destroyed.
  • No crashes due to stopped activities: It means if an activity is in the back stack,
then it doesn’t receive any LiveData stream.
  • Always up to date: It receives the latest data upon becoming active again.
  • No more manual life-cycling handle: Observers just observe relevant data and
don’t stop or resume observation. LiveData manages all of this under the hood.
  • Ensures your UI matches the data state: LiveData notifies the observer object
whenever lifecycle state changes. Instead of updating the UI every-time when the data
changes, your observer can update the UI every time there’s a change.


How to Configure:
So, enough of this theory lets see how we can use this in our Android app.
To use LiveData add the following dependency in the build.gradle file.


  1. implementation "android.arch.lifecycle:extensions:$current_version"
  2. annotationProcessor "android.arch.lifecycle:compiler:1.0.0"
What is MutableLiveData:
MutableLiveData is also a LiveData. Internally MutableLiveData extends LiveData class
and exposes the two methods publically which is setValue and getValue. With setValue,
you can set the new value and after that, it calls all the active observers with new modified
value. And last, with getValue, you can get the recent value stored inside the LiveData
instance.
Ways of Setting Data:
There are currently two ways of setting the value inside the LiveData instance.
The first one we’ve seen which is setValue and second one is postValue.
The setValue method is used to set data from Main Thread. If the code executed
inside the Worker Thread or Background Thread you can use the postValue method.


How to convert exposed MutableLiveData to LiveData?

  • Find any public MutableLiveData
  • Rename its variable to prefix mutable
  • Make it private
  • Add a public LiveData field to back it
Example
val error = MutableLiveData<String>()
to
private val mutableError = MutableLiveData<String>()
val error: LiveData<String> = mutableError

To get a better understanding of the above concept you can view the below-mentioned code

ISSUE :  #782  

PR : #794

Author : Govind Dixit

Tags : Fossasia, Codeheat, Open Event, Android, Kotlin


Comments

Popular posts from this blog

What is Blockchain Technology?

A  Blockchain , initially  Block chain , is a developing rundown of records called blocks, which are connected utilizing cryptography. Blockchains which are decipherable by people in general are broadly utilized by cryptocurencies. Private blockchains have been proposed for business utilize. Here, is a short story which will make you comfortable with the  Blockchain Technology . Mario and Luigi Mario needs to send $100 to his brother, Luigi, because Luigi being Luigi, he got into some debts at the other end of the world. Mario walks into the bank and says “I’d like to send $100 to Luigi”. The Cashier says “Account card please”, “ID please”, ”signature” and “done”. In this centralized situation, the bank is the *central* expert over Mario's and Luigi's cash. Both Mario and Luigi trust the bank to exchange the sum, and trust the numbers appeared on their bank account statements. They trust the bank in-spite of the way that all the bank needs to do is change

Why use Snackbar instead of Toasts?

Open Event Android   Why use Snackbar instead of Toasts? January 14, 2019 Problem Toasts are kind of deprecated as any user who has disabled notifications for an app won't be able to see toast as well. The Open Event Android App was using toast for displaying information. So the task is to completely remove toasts and use snackbar instead. Solution Why Snackbar? Snackbars provide lightweight feedback about an operation. They show a brief message at the bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other elements on screen and only one can be displayed at a time.They automatically disappear after a timeout or after user interaction elsewhere on the screen, particularly after interactions that summon a new surface or activity. Snackbars can be swiped off screen. The Difference Snackbar Contain a single line of text directly related to the operation performed. They may contain a text action,