Android Pattern MVVM

MVVM pattern definition

📌 MVVM stands for ModelViewViewModel.

MVVM associated with the TaskApp sample

View

  • It represents the UI of the application devoid of any Application Logic.
  • It observes the ViewModel.

ViewModel

  • It acts as a link between the Model and the View.
  • It’s responsible for transforming the data from the Model.
  • It provides data streams to the View.
  • It also uses hooks or callbacks to update the View.
  • It’ll ask for the data from the Model.

Model

  • This holds the data of the application.
  • It cannot directly talk to the View.
  • It’s recommended to expose the data to the ViewModel through Observables

Android implementation components

📢 The following components are required to implement the sample app with Android and Kotlin. We will have two implementations: Simple MVVM with a text view and Simple MVVM with Recycle View.

View Binding

  • View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module.
  • An instance of a binding class contains direct references to all views that have an ID in the corresponding layout.

📌 In most cases, view binding replaces findViewById.

ViewModel

  • The ViewModelclass is designed to store and manage UI-related data in a lifecycle conscious way.
  • The ViewModelclass allows data to survive configuration changes such as screen rotations.

Lifecycle

  • Lifecycle-aware components perform actions in response to a change in the lifecycle status of another component, such as activities and fragments. These components help you produce better-organized, and often lighter-weight code, that is easier to maintain.

LiveData

  • LiveDatais 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.
  • This awareness ensures LiveData only updates app component observers that are in an active lifecycle state.

Recycler View

  • RecyclerView makes it easy to efficiently display large sets of data.
  • You supply the data and define how each item looks, and the RecyclerView library dynamically creates the elements when they’re needed.

Implementing your adapter and view holder

  • Once you’ve determined your layout, you need to implement your Adapterand ViewHolder.
  • These two classes work together to define how your data is displayed.
  • The ViewHolderis a wrapper around a View that contains the layout for an individual item in the list.
  • The Adaptercreates ViewHolderobjects as needed, and also sets the data for those views. The process of associating views to their data is called binding.

MaterialCardView

  • This class supplies Material styles for the card in the constructor.
  • The widget will display the correct default Material styles without the use of a style flag.

Github Repositories

TaskApp MVVM

TaskApp MVVM RecycleView