Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

1 Live Data(Android Jetpack)

Live Data Section Summary(Short Note)


● Android LiveData is a lifecycle-aware, observable data holder class.
● As its name suggests, we use it to get live data.
● In other words, to get real time updates from the data sources .
● LiveData is an observable data holder class. Unlike a regular
observable, LiveData is lifecycle-aware, meaning it respects the
lifecycle of other app components, such as activities, fragments, or
services.

Some of the advantages of LiveData includes:


● No memory leaks
● Views always get the up to date data
● No crash due to stopped activities

Define a live data instance(inside the ViewModel class)


​ class MainActivityViewModel : ViewModel() {
​ var count = MutableLiveData<Int>()
​ ........
​ ........
​ }

Update the value of a live data instance.


​ class MainActivityViewModel : ViewModel() {
​ var count = MutableLiveData<Int>()

​ init {
​ count.value = 0
​ }

​ .......
​ }

Exam Warriors: A Digital Library


2 Live Data(Android Jetpack)

Observe a LiveData instance from a view(activity)


​ class MainActivity : AppCompatActivity() {
​ ......
​ private lateinit var viewModel:
MainActivityViewModel
​ override fun onCreate(savedInstanceState:
Bundle?) {
​ ........
​ ........
​ viewModel =
ViewModelProvider(this).get(MainActivityViewModel:
:class.java)
​ viewModel.count.observe(this, Observer {
​ binding.countText.text = it.toString()
​ })

​ ........
​ }
​ }

Frequently Asked Questions.

1) Where do we create/generate LiveData?


We usually define LiveData, inside ViewModel classes.
Also, supporting libraries like Room and Retrofit allows us to get data
directly in LiveData format.

2) From where do we observe LiveData?

Exam Warriors: A Digital Library


3 Live Data(Android Jetpack)

LiveData is a lifecycle-aware observable data holder class.


In android we have only 3 app components with lifecycles. Activities ,
fragments and services.
So, from activities, fragments and services we can observe LiveData.

3) What is the difference between RxJava and LiveData?


RxJava is not a lifecycle aware component.
So, data stream does not go off, when activity, fragment or service
becomes inactive.
As a result of that, memory leaks or crashes can happen.
Therefore, we have to write codes to dispose of them manually.
But, on the other hand, Android LiveData is aware of lifecycle status
changes.
And, they clean up themselves(stop emitting data) when their associated
lifecycle is destroyed.

4) What is MutableLiveData, what is the difference between


LiveData and MutableLiveData ?
MutableLiveData class is a subclass of LiveData class. In other words,
MutableLiveData child class has created by extending the parent LiveData
class.
A MutableLiveData instance can do everything a LiveData instnce can do
and more.
Data in a LiveData object are only readable. We cannot update those
values.
But, in the other hand, a Mutable LiveData object allows us to
change(update) its values.

Exam Warriors: A Digital Library


4 Live Data(Android Jetpack)

So, When we are creating our own live data(mostly in ViewModels), we


define them as MutableLiveData.
But, when we are getting live data from other libraries such as Room and
Retrofit we get them as LiveData.
We can easily transfer values between those two formats.

Exam Warriors: A Digital Library

You might also like