Android Architecture Patterns Model View View Model
MVVM Android Tutorial Theory
Introduction
•MVVM stands for ModelViewViewModel is the Google’s recommended architecture for building robust, production-quality apps.
- MVVM is a design pattern that guides you how to structure and organize app code so that it can be maintainable, testable and reusable while building Android apps.
Common Problems
•A typical Android app is constructed out of multiple app components, including activities, fragments, services, content providers and broadcast receivers.
•These app components are declared in the app manifest which is used by the Android OS to decide how to integrate your app into the overall user experience with their devices.
- As mobile devices are resource constrained, so at any time, the operating system may need to kill some apps to make room for new ones.
- These app components are ephemeral and their lifecycle (when they are created and destroyed) are not under developer control, we should not store any app data or state in your app components and your app components should not depend on each other.
How to Structure Apps?
•The most common mistake is all code is written in Activity or Fragment.
•Developer should focus on is separation concerns in his app.
•Any code that does not handle user interface should not be in Activity or Fragment.
- Keep them as lean as possible to avoid life cycle related problems.
•The second important principle is that we should drive our UI from a model, preferably a persistent model.
•Users won’t lose data if the OS destroys your app to free up resources and your app will continue to work even when a network connection is flaky or not connected.
•Models are independent from the Views and app components in your app, hence they are isolated from the lifecycle issues of those components.
• Keeping UI code simple and free of app logic makes it easier to manage.
Model View ViewModel Architecture
(1)Model:Code to fetch data from DB/Webserver
(2)View :Activity/Fragment Layout
(3) ViewModel: Exposes Data to View
View
•View component represents user interface of the application with out business logic.
•The View is responsible for handling layouts like binding data to widgets , starting activities/Fragments, Event Listeners(Button clicks),handling menus, displaying dialogs with out business logic.
ViewModel
•Acts as interface between View and Model components
•Contains the data required for the view.
•Uses Live Data(Observable data) to notify view about changes.
- View Model can’t inflate a layout or start an activity/fragment like view.
Model
•It holds the data required for application and can’t directly interact with View .
- Fetches data from different sources such as Database (Room DB/Sqlite),Rest API,Firebase etc. and exposes fetched data to ViewModel using observables.
Adding MVVM components to your App
Dependencies:
dependencies {
def lifecycle_version = “1.1.1”
def room_version = “1.1.1”
// ViewModel and LiveData
implementation “android.arch.lifecycle:extensions:$lifecycle_version”
// alternatively — just ViewModel
implementation “android.arch.lifecycle:viewmodel:$lifecycle_version” // use -ktx for Kotlin
// alternatively — just LiveData
implementation “android.arch.lifecycle:livedata:$lifecycle_version”
// alternatively — Lifecycles only (no ViewModel or LiveData)
implementation “android.arch.lifecycle:runtime:$lifecycle_version”
annotationProcessor “android.arch.lifecycle:compiler:$lifecycle_version”
// alternately — if using Java8, use the following instead of compiler
implementation “android.arch.lifecycle:common-java8:$lifecycle_version”
// optional — ReactiveStreams support for LiveData
implementation “android.arch.lifecycle:reactivestreams:$lifecycle_version”
// optional — Test helpers for LiveData
testImplementation “android.arch.core:core-testing:$lifecycle_version”
implementation “android.arch.persistence.room:runtime:$room_version”
annotationProcessor “android.arch.persistence.room:compiler:$room_version”
implementation “android.arch.persistence.room:guava:$room_version”
testImplementation “android.arch.persistence.room:testing:$room_version”
}
For detail info on Dependencies:
For Implementation various scenarios of MVVM ,Please find below links: