Design Converter
Functionalities
Last updated on Oct 24, 2023
Last updated on Jul 12, 2021
LiveData is the android architecture component that simplifies reactive programming for responsive UI development. In this article, you will learn about LiveData, its importance in android development, and how to work with it in Kotlin.
In Android development, Live Data is one of the core Android architecture components essential for reactive programming. It is a simple yet powerful component that changes your UI based on some data.
For Example, if you want to show the number of followers for the user profile in your social media application, with the dynamic values that change when someone follows or unfollows your account. These changes can be carried out with the help of the ViewModel.
Google has introduced the concept of LiveData to ease the working of ViewModel. Let us know more about it.
LiveData is a Library in Lifecycle 2.0, which is now a part of Android Jetpack (A set of components, tools, and guidance designed to accelerate app development). LiveData is an observable data holder class, which means it is aware of the lifecycle of other application components such as, activities, fragments, and services.
UI observes the LiveData objects to show the data on the screen. Therefore, when the LiveData changes, UI gets notified, and then UI updates itself with the LiveData changes. In a nutshell, LiveData synchronizes the UI with the updated data.
If the lifecycle status is STARTED or RESUMED, LiveData considers an observer to be in an active state. Only active observers are notified of lifecycle updates by LiveData. Inactive observers who have registered to watch the LiveData objects, on the other hand, are not notified of status updates.
LiveData is an important android architectural component because,
Therefore it plays an important role in the MVVM architectural pattern.
Here are the fundamental steps for getting started with LiveData Objects in Kotlin.
When the value stored in the LiveData object changes, the change is notified to all observers associated with the LiveData once they enter the resume state.
Let’s see how we can code for the above steps.
LiveData is a wrapper that can be used with any data, including the objects which can be observed from the UI components i.e. ViewModel via getter method as demonstrated in the example below.
1class NameViewModel : ViewModel() { 2// Create a LiveData with a String 3val currentName: MutableLiveData< String > by lazy { 4MutableLiveData() 5} 6// Rest of the ViewModel… 7}
In the above code, LiveData declared as val can be changed, but you can update the value of LiveData because it is MutableLiveData. Any changes in its value will notify all of its active observers.
To begin observing LiveData objects, go to the method onCreate(). This is because it ensures that the system doesn’t make redundant calls from an activity or fragment onResume method() and the activity and fragment must have data that it can display once it is active.
In general, LiveData notifies changes to the active Observers only when they receive updates on the data. However, there is one exception; observers receive updates as they transition from inactive to active status.
Further, if the observer changes inactive to active state a second time, in such a case it will be notified only if the value has changed since the last time it became active.
Here is a code sample that illustrates how to start observing LiveData objects.
1class NameActivity : AppCompatActivity() { 2// Use the ‘by viewModels()’ Kotlin property delegate 3// from the activity-ktx artifact 4private val model: NameViewModel by viewModels() 5override fun onCreate(savedInstanceState: Bundle?) { 6super.onCreate(savedInstanceState) 7// Other code to set up the activity… 8// Create the observer which updates the UI. 9val nameObserver = Observer< String> { newName -> 10// Update the UI, in this case, a TextView. 11nameTextView.text = newName 12} 13// Observe the LiveData, passing in this activity as the LifecycleOwner and the observer. 14model.currentName.observe(this, nameObserver) 15} 16}
LiveData lacks public methods for updating its stored values. As a result, you can use MutableLiveData to publicly update the value inside LiveData using the setValue() and postValue() methods.
You can update the value of the LiveData object as shown in the code sample below.
1button.setOnClickListener { 2val anotherName = “John Doe” 3model.currentName.setValue(anotherName) 4}
This is how you can use LiveData in reactive programming.
App users expect responsive UI components that immediately respond to the changes in related data for interactive user experience. LiveData library enables developers to make such changes to the UI to improve user satisfaction and brand experience.
Finding ways to simplify your Android development? Try DhiWise Kotlin Builder!!
DhiWise is a 100% developer-centric platform that supports Android architecture development components(MVVM). As a result, you will be able to build your app more quickly. Start using the DhiWise Kotlin builder and generate your code automatically.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.