● 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>() ........ ........ }
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.
Describe Android Application Architecture? 10 Marks 2. A. Explain Activity Life Cycle in Android? 7 Marks B. What Is An Intent? Define Types of Intents? 3 Marks