Open Event Android
Android Livedata and
MutableLiveData
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.
lifecycle LiveData and ViewModel, we can easily overcome these problems.
LiveData
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.
themselves when their associated life cycle destroyed.
- No crashes due to stopped activities: It means if an activity is in the back stack,
- 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
- Ensures your UI matches the data state: LiveData notifies the observer object
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.
To use LiveData add the following dependency in the build.gradle file.
- implementation "android.arch.lifecycle:extensions:$current_version"
- annotationProcessor "android.arch.lifecycle:compiler:1.0.0"
What is MutableLiveData:
you can set the new value and after that, it calls all the active observers with new modified
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.
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
To get a better understanding of the above concept you can view the below-mentioned code
Comments
Post a Comment