MVVM pattern definition
📌 MVVM stands for Model, View, ViewModel.
- It represents the UI of the application devoid of any Application Logic.
- It observes the
- It acts as a link between the
- It’s responsible for transforming the data from the
- It provides data streams to the
- It also uses hooks or callbacks to update the
- It’ll ask for the data from the
- This holds the data of the application.
- It cannot directly talk to the
- It’s recommended to expose the data to the
📢 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 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
ViewModelclass is designed to store and manage UI-related data in a lifecycle conscious way.
ViewModelclass allows data to survive configuration changes such as screen rotations.
- 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.
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.
- 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.
- Once you’ve determined your layout, you need to implement your
- These two classes work together to define how your data is displayed.
ViewHolderis a wrapper around a
Viewthat contains the layout for an individual item in the list.
ViewHolderobjects as needed, and also sets the data for those views. The process of associating views to their data is called binding.
- 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.