
Getting started with the New Android Architecture Components
During this year Google IO 17, The Android team announced lot of new stuffs for the Android Platform such as the availability of Android O etc. The one that really got Android Developers excited was the android Architecture component.
And the Like…
Before this time, i believe lots of beginner and intermediate developers like me where still writing all of their business logic in the Activity or fragment which is very difficult to ready and test. For Network request or Db queries the codes are then moved to AsynTask just for reducing the work load on the MainThread.
This year i started hearing of architecture patterns like MVP, MVC and MVVM which was to help in separation of concerns and to reduce the possibility of writing lots of boilerplate code and doing all the work in Activity or fragment. Yea! i had to learn MVP this really change my code base structure. But it didn’t go that easy.
So during Google IO 17, the new Architecture Components was released by the Android team because of the strong need for guide on architecture best practices.
What are the new Architecture Component?
A new collection of libraries that help you design robust, testable, and maintainable apps. Start with classes for managing your UI component Lifecycle and handling data persistence. This gives you time to focus on your core App functionality and logic.
These are the Components:
Lifecycle: It’s interfaces that let you build lifecycle-aware components. It is a class that holds the information about the Lifecycle state of a component(Activity or fragment).
LiveData: Lifecycle aware observable component where the Observer can specify a Lifecycle in which it should observe.
ViewModel: This will help to store and manage UI-related data so that the data survives configuration changes such as screen rotations.
LifecycleObserver: To help you build lifecycle-aware component that can automatically adjust their behavior based on the current Lifecycle of an activity or fragment.
LifecyclerOwner: Core interface for components that have a Lifecyle(Activities, Fragments and Processes).
Building Android Apps with the new Architecture Component
Using this new architecture component, we will be building Google Keep clone Android App which will help you create,edit and update notes. For this stage we will cover three architecture component with are Room, LiveData and ViewModel. Get complete code here

What is Room?
Room abstracts away some of the underlying implementation details related to working with raw SQL. Room to observe changes to data stored in a database, and expose these changes as LiveData objects. With Room, you no longer need to use Cursors and Loaders.
There are 3 major components in Room:
Database, Entity and Data Access Object (DAO).
Implementing ROOM
- Add the Google Maven Repository to the top level build.gradle:
2. Add the Room dependencies to your app/build.gradle:
3. At this point we will require an Entity which refers to a table that will store the user Task in the Keep App. This component represents a class that holds a database row. This class is created with @Entity annotation with the table name and @PimaryKey to uniquely identify a field. @ColumnInfo is used to rename fields if you want them to have different names.
Note: Getters and setters are ignored for brevity(shortness), but they’re required for Room to work.
4. Create TaskDao interface as a Data Access Object (DAO) and annotated it with @Dao annotations. DAOs are the main component of Room and are responsible for defining the methods that access the database. annotations such as @Query, @Delete, @Insert, @Update. The @Query annotation can take a SQL structured query.
5. Create an abstract class in this case TaskDatabase and annotate it with the @Database annotation. Here you set the database properties such the Entity and the database version. This class should extend RoomDatabase.
6. Create a singleton TaskDatabase object that uses Room.databasebuilder(..) to create an instance. We could also create an in-memory database by using Room.inMemoryDatabaseBuilder(..) method. For simplicity and less stress i used Dagger to create the singleton of instance and injecting it when needed.
What are ViewModels and LiveData?
View Model
View Model actually came from the MVVM pattern designed by Microsoft in around 2005.
The ViewModel class is designed to store and manage UI-related data so that the data survives configuration changes such as screen rotations.
- A
ViewModelcan retain its state across Activity configuration changes. The data it holds is immediately available to the next Activity instance without needing to save data inonSaveInstanceState()and restore it manually. - A
ViewModeloutlives the specific Activity or Fragment instances.
LiveData
LiveData is a data holder class that keeps a value and allows this value to be observed. It gives you the ability to receive automatic database updates which can be difficult to achieve by just using a standard SQLiteDatabase.
7. Creating a ViewModel for the KeepClone app
- Create a class called
TaskListViewModel.Make sure it extendsViewModelfrom the Architecture components class. - Add the
LiveDatavariable to theTaskListViewModel. The TaskRepository variable will be injected using Dagger. TheTaskListViewModelclass should look like this now:
Note: I decide to use RxJava to observe and subscribe to the repository event to be able to listen to the event results.
8. Now in the TaskListFragment, we will be loading the list of tasks from the ViewModel .
Summary
The new Android Architecture Component solve many problems most Android developers had such as managing screen configuration , observing changes from the database in case of update with LiveData and Seperation of concerns by decoupling your business logic from you Views with ViewModels.
Reference
https://riggaroo.co.za/android-architecture-components-looking-room-livedata-part-1/
Goodbye to Loaders and Cursor!!
your feedback can save a soul.