SlideShare a Scribd company logo
How Android Architecture Components can Help You Improve Your App’s Design?
How Android Architecture Components can Help You
Improve Your App’s Design?
With so many app architectures around
weren’t you confused when it came to
picking the right pattern for your app?
Whether to choose Model View Presenter
(MVP), Model View Controller (MVC),
Model-View-ViewModel (MVVM), some
other pattern, or even no architectural
pattern at all?
Until Google I/O 2017, there was no clue from Google too! Or rather, the tech giant
never made any recommendations on any specific architecture for Android app
development.
However, in response to popular demand from Android app developers, the Android
Framework team has come out with an opinionated guide to architecting Android
apps. Along with the guide they have developed a companion set of architecture
components.
In today’s post we would be talking about Android architecture components, why you
need them and how they help you to improve your apps. So let’s get started!
Why do you need architecture components?
Well, according to Lyla, developer advocate at Android, the architecture components can
help you with common development challenges. These components can:
 persist data;
 manage lifecycle;
 make your app modular;
 help you avoid memory leaks;
 prevent you from writing boilerplate code.
You can either use these components separately or together. A very simple app needs a
database and a robust app UI that modifies, requests or generates data for database and
also displays data from the database. So, the new components make this process that is
required to make a basic app, function, quite easy. Moreover, these components are
designed to work together like building blocks. With the release of Android P Developer
Preview 1, Google is trying to make the app design more systematic with the new
architecture components.
A brief introduction to the architecture components and
how they can enable you to enhance your app’s design
So, what are Android architecture components? Well, a set of libraries that would enable
you to design robust, testable and maintainable apps. The components are as follows:
 Room
 LiveData
 Lifecycle-aware components
 ViewModel
 Paging
Room
Room is a robust SQL object-mapping library that you can use to tackle the database. In
order to set up the tables using Room, you can define a Plain Old Java Object, or a POJO
and then mark this POJO with an @Entity annotation as shown below:
@Entity
public class Trail {
public @PrimaryKey String id;
public String name;
public double kilometers;
public int difficulty;
}
Now for each POJO you would need to define a DAO or a Database Access Object. The
annotated methods represent the SQLite commands you would need to interact with your
POJO’s data. (Android Developers official website is the source of all the screenshots or
images used within this article.)
Room
Along with providing a fluent database access,
Room also perform the following two
important functions:
It automatically converts your POJO objects
into the corresponding database tables and
back again.
It verifies your SQLite at compile time, so that
if you make a mistake like referencing a
column that is not actually in the database, it
will throw a helpful error.
Now that you have a Room database, you can
use another new architecture component,
called LiveData, to monitor changes in the
database.
Live Data
LiveData is an observable data holder. It implies that LiveData holds data and
lets you know by a notification when the data changes so that you can change
the UI accordingly.
It is an abstract class that you can extend or for simple cases you can use the
MutableLiveData class. Whenever you update the value of the MutableLiveData
with a call to set value, it would then trigger and update in your UI.
Room is designed for supporting LiveData and this a very powerful feature
indeed. In case, you want to use them together, just modify your DAO to return
objects that are wrapped with the LiveData class. Room will create a LiveData
object observing the database.
Now, to update your UI using LiveData and Room, you can write code as
follows:
Live Data
The final outcome is that every time your Room database updates, the data in
your LiveData changes simultaneously which in turn automatically triggers UI
updates.
This reminds us of another astounding feature of LiveData, that is, it is a
Lifecycle-aware component.
Lifecycle-aware components
According to the definition in Android developers’ site, Lifecycle-aware components
perform actions in response to a change in the lifecycle status of another component,
such as activities and fragments. These components help you produce better-organized,
and often lighter-weight code, that is easier to maintain.
Now, LiveData is Lifecycle-aware because it can find out when your activity is on-screen,
off-screen or destroyed through the power of lifecycle observation. Thus, it makes sure
that it does not send database updates to an inactive UI.
Lifecycle-aware components have two interfaces:
1. Lifecycle Owner; and
2. Lifecycle Observer.
3. Lifecycle Owners: These are objects with lifecycles, such as, activities and
fragments.
4. Lifecycle Observers: This interface, on the other hand, observes Lifecycle Owners
and are informed of lifecycle changes.
Lifecycle-aware components
Let’s put a quick glance at the simplified code for LiveData that is a Lifecycle observer too.
The methods that are annotated with @OnLifecycleEvent manage initialization and
decimate when the associated Lifecycle Owner starts or stops.
This in turn enables LiveData objects to manage their own setup and tear down; this
allows the UI components to observe the LiveData, and the LiveData components to
observe the Lifecycle Owners.
Lifecycle-aware components
Android Developers team suggests that Android library
designers can use this same lifecycle observation code to
call up setup and tear down functions automatically for
their own libraries.
Now, let’s take a quick peek at another frequently
confronted problem. While your app is used, it undergoes
various configuration changes, which destroys and rebuilds
the activity. We don’t suggest you to tie the initialization of
LiveData to the activity lifecycle. This is because it would
lead to the re-execution of code unnecessarily.
For example, if you rotate the phone, your database query
is executed. So, what should you do? Well, the good news
is that there is a solution! You can rather put your LiveData
or any other data associated with the UI in a ViewModel.
ViewModel
ViewModels are objects that provide data for UI components and remain unaffected by
configuration changes.
Every time you want to create a ViewModel object, you would need to extend the
ViewModel class. Then, you have to put all the data needed for your activity UI into the
ViewModel as shown in the screenshot below:
Now on, if your activity is recreated owing to a configuration change, your app won’t re-
query the database because you have already cached data for the UI within the
ViewModel.
ViewModel
If you need to create your activity or fragment, you can now get a reference to the
ViewModel and use it (see screenshot below).
When you create an activity a ViewModel is generated for your activity for the first time.
When you request again a ViewModel, the original ViewModel with the UI data cache is
received. This ensures that there is no database calls needlessly anymore.
Paging
Now let’s assume that we have a list of users instead of a single user. Many applications
require loading a lot of information from the database. Database queries can take a long
time to run and consume a lot of memory.
To address all of these issues, Google recently released an architecture component, called
Paging, which streamlines the process of requesting data as and when you need it.
The main components of the Paging library are:
1. PagedList Adapter;
2. PagedList;
3. DataSource; and
4. LivePageListBuilder
You can get a detailed view of the Paging library at the Android developers site.
Summing up
Hope you too are super excited to know about this new architecture shininess, which will
enable you to enhance the design of your applications.
Do you have any Android app idea? You can talk with our Android app development
experts to transform it into reality.
Originally Published at: https://goo.gl/mRBq7w

More Related Content

What's hot (19)

PPT
Day: 2 Environment Setup for Android Application Development
Ahsanul Karim
 
PPTX
Codess shreya sharma
ShreyaSharma247
 
PPTX
Android Study Jam 2
DSC GVP
 
PDF
Android dev o_auth
fantasy zheng
 
PPTX
Android Development : (Android Studio, PHP, XML, MySQL)
Kavya Barnadhya Hazarika
 
PPT
Day 3: Getting Active Through Activities
Ahsanul Karim
 
PDF
"Universal programming recipes", Kateryna Trofimenko
Badoo Development
 
PDF
AndroidManifest
Ahsanul Karim
 
PDF
Sensors in Android (old)
Ahsanul Karim
 
PPTX
Power Apps community call-June 2020
Microsoft 365 Developer
 
PDF
Ui layout (incomplete)
Ahsanul Karim
 
PPTX
Android Development for Beginners with Sample Project - Day 1
Joemarie Amparo
 
PDF
Day 1 Android: Before Getting Started
Ahsanul Karim
 
PDF
Android Development: Build Android App from Scratch
Taufan Erfiyanto
 
PPTX
From React to React Native - Things I wish I knew when I started
sparkfabrik
 
PPTX
Beginning android
Igor R
 
PDF
Firebase. Предмет и область применения — Тимур Ахметгареев
Peri Innovations
 
PDF
Os eclipse-androidwidget-pdf
weerabahu
 
PPTX
Introduction to Android Development
Prof. Erwin Globio
 
Day: 2 Environment Setup for Android Application Development
Ahsanul Karim
 
Codess shreya sharma
ShreyaSharma247
 
Android Study Jam 2
DSC GVP
 
Android dev o_auth
fantasy zheng
 
Android Development : (Android Studio, PHP, XML, MySQL)
Kavya Barnadhya Hazarika
 
Day 3: Getting Active Through Activities
Ahsanul Karim
 
"Universal programming recipes", Kateryna Trofimenko
Badoo Development
 
AndroidManifest
Ahsanul Karim
 
Sensors in Android (old)
Ahsanul Karim
 
Power Apps community call-June 2020
Microsoft 365 Developer
 
Ui layout (incomplete)
Ahsanul Karim
 
Android Development for Beginners with Sample Project - Day 1
Joemarie Amparo
 
Day 1 Android: Before Getting Started
Ahsanul Karim
 
Android Development: Build Android App from Scratch
Taufan Erfiyanto
 
From React to React Native - Things I wish I knew when I started
sparkfabrik
 
Beginning android
Igor R
 
Firebase. Предмет и область применения — Тимур Ахметгареев
Peri Innovations
 
Os eclipse-androidwidget-pdf
weerabahu
 
Introduction to Android Development
Prof. Erwin Globio
 

Similar to How Android Architecture Components can Help You Improve Your App’s Design? (20)

PDF
Android Architecture Components
Darshan Parikh
 
PPTX
Android architectural components
Muhammad Ali
 
PDF
Android Architecture Components
Gabriel Samojło
 
PPTX
Android Architecture Components
BurhanuddinRashid
 
PPTX
The Best Way to Become an Android Developer Expert with Android Jetpack
Ahmad Arif Faizin
 
PDF
Android architecture components - how they fit in good old architectural patt...
DroidConTLV
 
PDF
Android Jetpack - Google IO Extended Singapore 2018
Hassan Abid
 
PPTX
Model viewviewmodel2
Suraj Kulkarni
 
PPSX
Android architecture components
azzeddine chenine
 
PPTX
Architecture components, Константин Марс, TeamLead, Senior Developer, DataArt
Alina Vilk
 
PPTX
Android Architecture Components - Guy Bar on, Vonage
DroidConTLV
 
PPTX
Android Architecture - Khoa Tran
Tu Le Dinh
 
PDF
Handling Lifecycles in a Jetpack way
Han Yin
 
PDF
Building Modern Apps using Android Architecture Components
Hassan Abid
 
PDF
Android Architecture Components with Kotlin
Adit Lal
 
PPTX
MVVM Presentation.pptx
AsfandyarZaidi
 
PDF
Architecture Components
DataArt
 
PDF
Architecture components - IT Talk
Constantine Mars
 
PDF
Architecture Components
Sang Eel Kim
 
DOCX
Android Application Components with Implementation & Examples
Dr. Chandrakant Divate
 
Android Architecture Components
Darshan Parikh
 
Android architectural components
Muhammad Ali
 
Android Architecture Components
Gabriel Samojło
 
Android Architecture Components
BurhanuddinRashid
 
The Best Way to Become an Android Developer Expert with Android Jetpack
Ahmad Arif Faizin
 
Android architecture components - how they fit in good old architectural patt...
DroidConTLV
 
Android Jetpack - Google IO Extended Singapore 2018
Hassan Abid
 
Model viewviewmodel2
Suraj Kulkarni
 
Android architecture components
azzeddine chenine
 
Architecture components, Константин Марс, TeamLead, Senior Developer, DataArt
Alina Vilk
 
Android Architecture Components - Guy Bar on, Vonage
DroidConTLV
 
Android Architecture - Khoa Tran
Tu Le Dinh
 
Handling Lifecycles in a Jetpack way
Han Yin
 
Building Modern Apps using Android Architecture Components
Hassan Abid
 
Android Architecture Components with Kotlin
Adit Lal
 
MVVM Presentation.pptx
AsfandyarZaidi
 
Architecture Components
DataArt
 
Architecture components - IT Talk
Constantine Mars
 
Architecture Components
Sang Eel Kim
 
Android Application Components with Implementation & Examples
Dr. Chandrakant Divate
 
Ad

Recently uploaded (20)

PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Biography of Daniel Podor.pdf
Daniel Podor
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Ad

How Android Architecture Components can Help You Improve Your App’s Design?

  • 2. How Android Architecture Components can Help You Improve Your App’s Design? With so many app architectures around weren’t you confused when it came to picking the right pattern for your app? Whether to choose Model View Presenter (MVP), Model View Controller (MVC), Model-View-ViewModel (MVVM), some other pattern, or even no architectural pattern at all?
  • 3. Until Google I/O 2017, there was no clue from Google too! Or rather, the tech giant never made any recommendations on any specific architecture for Android app development. However, in response to popular demand from Android app developers, the Android Framework team has come out with an opinionated guide to architecting Android apps. Along with the guide they have developed a companion set of architecture components. In today’s post we would be talking about Android architecture components, why you need them and how they help you to improve your apps. So let’s get started!
  • 4. Why do you need architecture components? Well, according to Lyla, developer advocate at Android, the architecture components can help you with common development challenges. These components can:  persist data;  manage lifecycle;  make your app modular;  help you avoid memory leaks;  prevent you from writing boilerplate code. You can either use these components separately or together. A very simple app needs a database and a robust app UI that modifies, requests or generates data for database and also displays data from the database. So, the new components make this process that is required to make a basic app, function, quite easy. Moreover, these components are designed to work together like building blocks. With the release of Android P Developer Preview 1, Google is trying to make the app design more systematic with the new architecture components.
  • 5. A brief introduction to the architecture components and how they can enable you to enhance your app’s design So, what are Android architecture components? Well, a set of libraries that would enable you to design robust, testable and maintainable apps. The components are as follows:  Room  LiveData  Lifecycle-aware components  ViewModel  Paging
  • 6. Room Room is a robust SQL object-mapping library that you can use to tackle the database. In order to set up the tables using Room, you can define a Plain Old Java Object, or a POJO and then mark this POJO with an @Entity annotation as shown below: @Entity public class Trail { public @PrimaryKey String id; public String name; public double kilometers; public int difficulty; } Now for each POJO you would need to define a DAO or a Database Access Object. The annotated methods represent the SQLite commands you would need to interact with your POJO’s data. (Android Developers official website is the source of all the screenshots or images used within this article.)
  • 7. Room Along with providing a fluent database access, Room also perform the following two important functions: It automatically converts your POJO objects into the corresponding database tables and back again. It verifies your SQLite at compile time, so that if you make a mistake like referencing a column that is not actually in the database, it will throw a helpful error. Now that you have a Room database, you can use another new architecture component, called LiveData, to monitor changes in the database.
  • 8. Live Data LiveData is an observable data holder. It implies that LiveData holds data and lets you know by a notification when the data changes so that you can change the UI accordingly. It is an abstract class that you can extend or for simple cases you can use the MutableLiveData class. Whenever you update the value of the MutableLiveData with a call to set value, it would then trigger and update in your UI. Room is designed for supporting LiveData and this a very powerful feature indeed. In case, you want to use them together, just modify your DAO to return objects that are wrapped with the LiveData class. Room will create a LiveData object observing the database. Now, to update your UI using LiveData and Room, you can write code as follows:
  • 9. Live Data The final outcome is that every time your Room database updates, the data in your LiveData changes simultaneously which in turn automatically triggers UI updates. This reminds us of another astounding feature of LiveData, that is, it is a Lifecycle-aware component.
  • 10. Lifecycle-aware components According to the definition in Android developers’ site, Lifecycle-aware components perform actions in response to a change in the lifecycle status of another component, such as activities and fragments. These components help you produce better-organized, and often lighter-weight code, that is easier to maintain. Now, LiveData is Lifecycle-aware because it can find out when your activity is on-screen, off-screen or destroyed through the power of lifecycle observation. Thus, it makes sure that it does not send database updates to an inactive UI. Lifecycle-aware components have two interfaces: 1. Lifecycle Owner; and 2. Lifecycle Observer. 3. Lifecycle Owners: These are objects with lifecycles, such as, activities and fragments. 4. Lifecycle Observers: This interface, on the other hand, observes Lifecycle Owners and are informed of lifecycle changes.
  • 11. Lifecycle-aware components Let’s put a quick glance at the simplified code for LiveData that is a Lifecycle observer too. The methods that are annotated with @OnLifecycleEvent manage initialization and decimate when the associated Lifecycle Owner starts or stops. This in turn enables LiveData objects to manage their own setup and tear down; this allows the UI components to observe the LiveData, and the LiveData components to observe the Lifecycle Owners.
  • 12. Lifecycle-aware components Android Developers team suggests that Android library designers can use this same lifecycle observation code to call up setup and tear down functions automatically for their own libraries. Now, let’s take a quick peek at another frequently confronted problem. While your app is used, it undergoes various configuration changes, which destroys and rebuilds the activity. We don’t suggest you to tie the initialization of LiveData to the activity lifecycle. This is because it would lead to the re-execution of code unnecessarily. For example, if you rotate the phone, your database query is executed. So, what should you do? Well, the good news is that there is a solution! You can rather put your LiveData or any other data associated with the UI in a ViewModel.
  • 13. ViewModel ViewModels are objects that provide data for UI components and remain unaffected by configuration changes. Every time you want to create a ViewModel object, you would need to extend the ViewModel class. Then, you have to put all the data needed for your activity UI into the ViewModel as shown in the screenshot below: Now on, if your activity is recreated owing to a configuration change, your app won’t re- query the database because you have already cached data for the UI within the ViewModel.
  • 14. ViewModel If you need to create your activity or fragment, you can now get a reference to the ViewModel and use it (see screenshot below). When you create an activity a ViewModel is generated for your activity for the first time. When you request again a ViewModel, the original ViewModel with the UI data cache is received. This ensures that there is no database calls needlessly anymore.
  • 15. Paging Now let’s assume that we have a list of users instead of a single user. Many applications require loading a lot of information from the database. Database queries can take a long time to run and consume a lot of memory. To address all of these issues, Google recently released an architecture component, called Paging, which streamlines the process of requesting data as and when you need it. The main components of the Paging library are: 1. PagedList Adapter; 2. PagedList; 3. DataSource; and 4. LivePageListBuilder You can get a detailed view of the Paging library at the Android developers site.
  • 16. Summing up Hope you too are super excited to know about this new architecture shininess, which will enable you to enhance the design of your applications. Do you have any Android app idea? You can talk with our Android app development experts to transform it into reality. Originally Published at: https://goo.gl/mRBq7w