Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Android Architecture Components 1.0: Lifecycle, LiveData, ViewModel and Room

Android Architecture Components 1.0: Lifecycle, LiveData, ViewModel and Room

Leia em Português

This item in japanese

Google has made available Android Architecture Components 1.0, a collection of libraries to design “robust, testable, and maintainable apps.” The current components are Lifecycle, LiveData, ViewModel and Room with others on the way.

Google has created Architecture Components to help with “persist data, manage the lifecycle of components, write modular applications, defense against common errors, write less boilerplate code.” The main components are:

Lifecycle is an architectural component used to create activities and fragments aware of their lifecycle, capable of adjusting their behavior based on their state. Components can go through multiple states – initialized, created, started, resumed, destroyed – and the lifecycle methods are called to perform various operations when a state event is triggered. Lifecycle will hold information about the status of a component, dealing with changes in status, and relieving the component of this tasks. The purpose is to write simpler code, easier to understand.

LiveData is an observable data holder class. LiveData is aware of the lifecycle of a component and provides updates when they are in an active state. LiveData is recommended to build reactive UIs.

ViewModel is another architectural component used to hold and manage data for activities or fragments, data which is preserved when the UI component is recreated in response to various user actions such as screen rotation. ViewModel is meant to hold some data-related code, resulting in the simplification of UI controllers.

Room is an object-mapping layer storing data in SQLite. Room includes query verification during compilation, to mark queries that might be erroneous. Google recommends using Room instead of directly accessing SQLite because data can be cached locally and it is integrated with LiveData, making it part of the component’s lifecycle.

In the near future, a new component will be made available, PagedList, which currently is in alpha. PageList is useful to gradually load data without waiting for large database queries to finish. They are also working on other architectural components that will be announced in the coming months.

Google has published Architecture Guidelines that explain how to use these components. They say the libraries are “ready for production” use, recommending using them to build Android applications.

Rate this Article