Android MVVM View Model with Live data

The components of the Android architecture provide libraries for simplifying usual tasks. Architecture components help you structure your application in a robust, verifiable, and manageable way with less repetitive code.
July 26, 2023

Shruti Gupta

Promote this blog post!

Your opinion of this article? Publish it! It creates awareness of our work.

Promote this blog post!

Your opinion of this article? Publish it! It creates awareness of our work.

Introduction:

The components of the Android architecture provide libraries for simplifying usual tasks. Architecture components help you structure your application in a robust, verifiable, and manageable way with less repetitive code. During Google I/O, Google introduced architecture components that include LiveData and ViewModel for the facilitation of developing an Android app using the MVVM pattern.

MVVM using LiveData:

It is a container class for observable data. Unlike a normal one, LiveData is lifecycle aware, which means it respects the lifecycle of other application components, such as tasks, fragments, or services. This knowledge ensures that LiveData only updates observers for application components that are in an active lifecycle state. LiveData is an observable data holder. This allows the components in your app to be able to observe LiveData objects for changes without creating explicit and rigid dependency paths between them. This means it can hold a set of data that can be observed from other Android components like Activities, Fragments, and Services. It’s lifecycle-aware and mostly used to update UI from the ViewModel in MVVM architecture projects.

Benefits of LiveData:

  • No Memory Loss- Watchers are tied to lifecycle objects and clean up after themselves when their associated lifecycle gets destroyed.
  • No crashes due to interrupted activities- If the view is destroyed or back stacked, it will not update it. With this approach, accidents can get withdrawn.
  • No manual lifecycle management further- As LiveData automatically manages lifecycle changes in the user interface, it is lifecycle aware.

ViewModel:

ViewModel is an architectural component used to implement Android apps using MVVM. The ViewModel component is crucial in Android development, as it’s lifecycle-aware and used to implement the separation of concerns principle. ViewModel helps us to store, manage and prepare UI related data in a lifecycle conscious way. The main benefit of the ViewModel is that it is created the first time and if the Activity, Fragment or any other LifecycleOwner is destroyed or recreated by the system, the next time it starts it will receive the same ViewModel created before. It works like a Loader (AsyncTaskLoader and CursorLoader)

view-model

As we noticed in the image above, the ViewModel is kept alive even when the Activity is destroyed or reconstructed due to a configuration change for example. When the activity is finished the ViewModel calls the onCleared() method to clean up any remaining resource and avoid memory leaks.
We must keep in mind:

  • A ViewModel class shouldn’t have any reference to LifecycleOwner, like Activity or fragment, or to the context, this makes the unit testing easier
  • ViewModel object can contain LifecycleObservers , such as LiveData objects, but a ViewModel must never observe changes to lifecycle-aware observables, this has to be done on the LifecycleOwner
  • If the ViewModel needs the Application context it should extend AndroidViewModel and receive the Application on the constructor

We’re here to help!

Are you dealing with complex Sales Challenges? Learn how we can help.

Going a step further

If you are interested in this topic, these articles may be of interest to you.