App Architecture

Architecture components are a set of Android libraries that help you structure your app in a way that is robust, testable, and maintainable.

By learning to work with Architecture Components, you'll write apps with less boilerplate code and you'll see strategies for dealing with tricky issues involving lifecycles and persistence.

Android Architecture Components

  • LiveData LiveData is a lifecycle-aware observable data holder, which means it respects the lifecycle of other app components, such as Activity, Fragment or Service. Therefore, you do not have to handle the lifecycle manually, because LiveData will also automatically remove the observer when the Fragment receives onDestroy().

  • ViewModel The ViewModel class is a helper for MVVM pattern. It's designed to store and manage data necessary for UI. The ViewModel class allows data to survive configuration changes such as screen rotations, but ViewModel does not replace onSaveInstanceState().

  • Room A SQLite object mapping library. It's an abstract layer over SQLite that significantly simplifies query building. Its possibilities are amazing and oriented towards the observer pattern. Now, it's easier to write a query using annotations.

MVVM

Architecture components are based on the Model-View-View-Model software architecture pattern an evolution of the Model-View-Presenter (MVP) pattern where the view-model knows nothing of the view which is derived from the classic Model-View-Controller pattern (MVC).

MVVM

MVVM allows for a seperation of user interface (UI or view) logic via markup and GUI code from the business and back-end logic (the data model). The View-Model is a value converter, it exposes data objects from the model to the view to facilitate presentation. It is more model than view and handles most of the view's display logic.

  • Model In MVVM pattern, the model layer is identical to that in MVC and MVP. It is responsible for managing business logic and fetching data from server or database. It provides the required information to the View-Model layer. Refers to either a domain model or to the data access layer.

  • View The view layer represents user interface elements and is responsible for displaying data. The View observes data exposed by the View-Model. Similarly to the MVP pattern, Activities and Fragments belong to the view layer together with control systems in XML files.

  • View-Model ViewModel is the responsible connection between the data layer and the view layer. Its task is to provide methods, commands and other properties that help maintain the view state, as well as manage the model as a result of actions in the view and handle events in the view. View-Model does not contain information about the view and is completely independent of the views.

App Architecture

Here's a summary explanation of the different classes in the diagram:

UI Controllers (Activities/Fragments) - In charge of displaying the view and passing on UI events.

ViewModels and LiveData - represent the data needed by the view. Requests data from the repository.

Repository - Single source of truth for the app's data. Acts as a clean API for the UI to communicate with. Mediator between data sources.

Remote Network Data Source - manages data from a remote data source, such as a REST server.

Model - manages local data stored in the database.

Note This architecture stresses that each class in the diagram only stores a reference to the class or classes directly "below it" and not any classes above it. This means the ViewModel class will store a reference to the Repository class, but not about the UI controller class above it or the Remote Data Source class two levels below it.

MVVM might be overkill for simple projects.

results matching ""

    No results matching ""