LiveData - An Observable Data Holder
Managing UI-updates in Android is a challenging task because only the view class can update the UI (Activity, Fragment). Implementing an architecture pattern in our project brings many changes, such as separating business logic from UI logic and improving code readability and maintainability.
When we make changes in the model, we must immediately update the UI manually; otherwise, the updated data will not display to the user, adding extra responsibility for the developer.
Manually updating the UI is also challenging because before the UI is updated, we must ensure that the view is still active. When an update occurs, if the view is finishing or not active, it can cause the app to crash
LiveData: An observable data holder class that wraps around another class (POJO class) that contains the information for the view.
LiveData is created inside the ViewModel and the ViewModel stores the updated value inside the LiveData.
LiveData works reactively, allowing the view to observe changes whenever the data in the holder class is updated. The view will update itself, but only while the view is still active.
LiveData is used to update the UI automatically whenever the dataset changes, eliminating the need for manual updates by the developer. LiveData is lifecycle-aware; it ensures updates occur while the UI is active, which prevents app crashes. It helps in building robust applications.
Setting Up the ViewModel
The ViewModel contains the MutableLiveData, which holds the object, and its value is subject to change - hence the term mutable whereas the LiveData value is immutable. Once the value is set, it can't be changed.
We can expose MutableLiveData directly to the view, but we avoid doing so because we want only the ViewModel to modify and prepare the latest dataset for the view, even the view class should not be allowed to change the dataset. (separating UI and business logic)
Connecting View with the ViewModel
Let's see how to connect MainActivity view with the ViewModel
Recommended by LinkedIn
ViewBinding: We are using ActivityMainBinding to access the corresponding java object for the view, Once ViewBinding is enabled in app-level gradle The library will automatically generate the binding file that contains the corresponding java objects, so thus eliminating the need to use findViewById(), ActivityMainBinding class is created at compile time that we can use directly.
The instance of ViewModel: We use the ViewModelProviders constructor to create an instance of the ViewModel, ViewModel Provider is a factory that produces the ViewModel object, It uses the Factory Design Pattern to create a unique instance of the ViewModel, only if the object is not created already.
Observing LiveData: We call viewModel.liveData.observe() and pass the MainActivity reference, livedata will subscribe to the lifecycle of the activity and it will not try to update the UI when the activity is inactive or finished. additionally onChanged() method will be called every time the dataset is changed.
Why Use LiveData
Lifecycle Aware: This prevents memory leaks and crashes as the livedata is lifecyle aware.
Automatic UI Updates: The UI updates automatically when the data is changed without needing to update the UI manually.
Loose Coupling: LiveData allows your activity to be loosely coupled with the ViewModel and does not pass the activity reference to the ViewModel.
Conclusion:
In this blog, we explored how LiveData can be used to manage UI state effectively, ensuring automatic updates and preventing crashes by being lifecycle-aware.
By Implementing the LiveData and ViewModel, you can create robust applications
If you feel this blog is helpful, please share it with your friends.