Android Application Architecture - MVVM
Android Application Architecture
The Architecture of the app is like the Blue Print of your house. It provides the basic bones and structure of your app. The well-thought-out architecture can make your code maintainable for years and help your team collaborate.
The idea is to learn how to structure android app and the benefits that come with this design.
ViewModel and LiveData classes will hide some of the complexity of lifecycle and help you avoid lifecycle related errors. In some case if you architecture your app correctly you won't even need to worry about lifecycle related issues in first place.
Finally, Data Binding helps to remove a bunch of boilerplate codes.
In Lifecycle we learn about how to properly handle activity and fragment lifecycle to avoid memory leaks, rotation issues and data loss.
We can solve this issues by using : onSaveInstanceState(outState: Bundle): Unit
and onRestoreInstanceState()
OnSaveInstanceState bundles has their own issues, they require extra code to store the state in the bundle and the logic to retrieve that state and they're also meant to be kept pretty small, well below 100 kb (storing too much data can throw exception)
But the better way to solve this particular problem, using classes from Lifecycle library.
We can solve this issues by using : onSaveInstanceState(outState: Bundle): Unit
and onRestoreInstanceState()
OnSaveInstanceState bundles has their own issues, they require extra code to store the state in the bundle and the logic to retrieve that state and they're also meant to be kept pretty small, well below 100 kb (storing too much data can throw exception)
But the better way to solve this particular problem, using classes from Lifecycle library.
Lifecycle Library includes:
- Lifecycle
- Lifecycle Observer
- Lifecycle Owner
- ViewModel
- LiveData
ViewModel and LiveData are designed keeping in regard application architecture in mind.
Application architecture is the better way of designing of an application's classes and relationships between them such that the code base is more organised, performative in particular scenarios and easier to work with. For ex. just like architecting your house with different rooms.
Architecting and app does with code and classes the same like architecting your house with rooms.
There's no right way to architect an app much like there's no empirically right way to architect the house.
There are many different architecture styles for android app each with different strengths and weakness. Some styles can also overlap. They fit different application needs team sizes, team dynamics and more.
Check out the Android Architecture Blueprint repository to know more.
MVVM
Here we will be learning about single- multi purpose architectural pattern called MVVM (Model-View-ViewModel)In a house rooms have different specific purposes, kitchen for cutting food, home office for working or bathroom to bath, etc... Just like while architecting an app we have classes with specific purposes as well.
Separation of Concerns
The software design principles that we'll be using is called Separation of Concerns.This says that the app should be divided into classes each that have separate responsibilities.
Architecture gives you guidelines to figure out which classes should have what responsibility in your app.
We'll we working with 3 classes:
- UI Controller
- View Model
- LiveData
UI Controller
- where Activity/Fragment are
- responsible for any UI related tasks
- displaying views, capturing user inputs
- By design UI controllers execute all of the draw commands that put our views on screen.
- Also when UI event happens like when a user presses a button, it's UI controller that gets notified first. But we want to limit the UI controller to only limit to UI related tasks.
- So we should take out all the decision-making power out of the UI controller.
- So when the UI Controller is responsible for drawing a text to you to the screen it is not responsible for the calculations and the processing that decides what actual text to draw. And while the UI controller that know when the button has been pressed, it's gonna immediately pass that information along to the Second class in out architecture called ViewModel.
ViewModel
- this class will do the actual decision making
- the purpose of the ViewModel is to hold the specific data needed to display the fragment or activity it's associated with.
- ViewModel may do simple calculations and transformations on that data so that it is ready to be displayed by the UI controller
- The ViewModel will contain the instance of 3rd class LiveData
- ViewModel never references fragments, activities or views.
LiveData
- Crucial for communicating information for the viewModel to the UI controller that it should update and redraw the screen
- Holds all of the data needed for the UI and prepare it for display.
Comments
Post a Comment