Android Architecture
Components
Room, LiveData & ViewModel
Carlos Daniel Munoz - Android Dev
@cdmunozi
[email protected]
August 9th - 2017
@MedellinAndroid
Slide 2
Slide 2 text
New architecture guidelines
for Android Development
Slide 3
Slide 3 text
3
The journey ...
Slide 4
Slide 4 text
4
Android Development Process
Slide 5
Slide 5 text
5
Android Development Process
● Put all your code into Activities files
Slide 6
Slide 6 text
6
Android Development Process
● Put all your code into Activities files
● Occasionally, move some stuff into AsyncTasks if error
NetworkOnMainThreadException
Slide 7
Slide 7 text
7
Android Development Process
● Put all your code into Activities files
● Occasionally, move some stuff into AsyncTasks if error
NetworkOnMainThreadException
● Unit tests and instrumentation tests → hard to do, read and
maintain
9
Had Google and
Android Dev team
shared some
guidance?
Slide 10
Slide 10 text
10
No
:(
Slide 11
Slide 11 text
Google I/O 2017
Android Architecture Components - AAC
Slide 12
Slide 12 text
12
What are AAC framework?
Slide 13
Slide 13 text
13
What are AAC framework?
● Libraries
Slide 14
Slide 14 text
14
What are AAC framework?
● Libraries
● Guidelines
Slide 15
Slide 15 text
15
What are AAC framework?
● Libraries
● Guidelines
● Common scenarios in apps dev
Slide 16
Slide 16 text
16
What are AAC framework?
● Libraries
● Guidelines
● Common scenarios in apps dev
● Aims:
Slide 17
Slide 17 text
17
What are AAC framework?
● Libraries
● Guidelines
● Common scenarios in apps dev
● Aims:
○ Reduce boilerplate
Slide 18
Slide 18 text
18
What are AAC framework?
● Libraries
● Guidelines
● Common scenarios in apps dev
● Aims:
○ Reduce boilerplate
○ Reduce repetitive code
Slide 19
Slide 19 text
19
What are AAC framework?
● Libraries
● Guidelines
● Common scenarios in apps dev
● Aims:
○ Reduce boilerplate
○ Reduce repetitive code
○ Focus on core features
Slide 20
Slide 20 text
20
Basic blocks of AAC
Slide 21
Slide 21 text
21
Basic blocks of AAC
● Room
Slide 22
Slide 22 text
22
Basic blocks of AAC
● Room → A SQLite object mapper (ORMLite - GreenDao)
Slide 23
Slide 23 text
23
Basic blocks of AAC
● Room → A SQLite object mapper (ORMLite - GreenDao)
● LiveData
Slide 24
Slide 24 text
24
Basic blocks of AAC
● Room → A SQLite object mapper (ORMLite - GreenDao)
● LiveData → A Lifecycle aware observable core component.
Slide 25
Slide 25 text
25
Basic blocks of AAC
● Room → A SQLite object mapper (ORMLite - GreenDao)
● LiveData → A Lifecycle aware observable core component.
● ViewModel
Slide 26
Slide 26 text
26
Basic blocks of AAC
● Room → A SQLite object mapper (ORMLite - GreenDao)
● LiveData → A Lifecycle aware observable core component.
● ViewModel → The communication points with the rest of the application
for Activities / Fragments. They are UI code free and outlive the activity or
fragment.
Slide 27
Slide 27 text
27
Basic blocks of AAC
● Room → A SQLite object mapper (ORMLite - GreenDao)
● LiveData → A Lifecycle aware observable core component.
● ViewModel → The communication points with the rest of the application
for Activities / Fragments. They are UI code free and outlive the activity or
fragment.
● Lifecycle
Slide 28
Slide 28 text
28
Basic blocks of AAC
● Room → A SQLite object mapper (ORMLite - GreenDao)
● LiveData → A Lifecycle aware observable core component.
● ViewModel → The communication points with the rest of the application
for Activities / Fragments. They are UI code free and outlive the activity or
fragment.
● Lifecycle → contains information about the lifecycle state of a
component (for instance an Activity).
Slide 29
Slide 29 text
29
Basic blocks of AAC
● Room → A SQLite object mapper (ORMLite - GreenDao)
● LiveData → A Lifecycle aware observable core component.
● ViewModel → The communication points with the rest of the application
for Activities / Fragments. They are UI code free and outlive the activity or
fragment.
● Lifecycle → contains information about the lifecycle state of a
component (for instance an Activity).
● Lifecycle owner
Slide 30
Slide 30 text
30
Basic blocks of AAC
● Room → A SQLite object mapper (ORMLite - GreenDao)
● LiveData → A Lifecycle aware observable core component.
● ViewModel → The communication points with the rest of the application
for Activities / Fragments. They are UI code free and outlive the activity or
fragment.
● Lifecycle → contains information about the lifecycle state of a
component (for instance an Activity).
● Lifecycle owner → Interface - Annotations
@OnLifecycleEvent(Lifecycle.Event.ON_START)
Slide 31
Slide 31 text
31
Basic blocks of AAC
● Room → A SQLite object mapper (ORMLite - GreenDao)
● LiveData → A Lifecycle aware observable core component.
● ViewModel → The communication points with the rest of the application
for Activities / Fragments. They are UI code free and outlive the activity or
fragment.
● Lifecycle → contains information about the lifecycle state of a
component (for instance an Activity).
● Lifecycle owner → Interface - Annotations
@OnLifecycleEvent(Lifecycle.Event.ON_START)
● Lifecycle observer
Slide 32
Slide 32 text
32
Basic blocks of AAC
● Room → A SQLite object mapper (ORMLite - GreenDao)
● LiveData → A Lifecycle aware observable core component.
● ViewModel → The communication points with the rest of the application
for Activities / Fragments. They are UI code free and outlive the activity or
fragment.
● Lifecycle → contains information about the lifecycle state of a
component (for instance an Activity).
● Lifecycle owner → Interface - Annotations
@OnLifecycleEvent(Lifecycle.Event.ON_START)
● Lifecycle observer → Interface - getLifecycle(): LifeCycleRegistry
Slide 33
Slide 33 text
Android Architecture Components - AAC
Can be used in isolation. Work really well when used
together, though.
Slide 34
Slide 34 text
34
Using Architecture Components
Slide 35
Slide 35 text
35
Using Architecture Components
● App → Events Scheduler
41
What is Room?
● New way to create databases in Android
● ORM between Java and SQLite
● Less verbose
● No more Cursors
● No more Loaders
● Room isn’t a fully-fledged ORM (no complex nesting objects)
Slide 42
Slide 42 text
42
How to query data?
Slide 43
Slide 43 text
43
How to query data?
● Using LiveData → exposes a stream of events that we can
subscribe to receive updates for asynchronously
Slide 44
Slide 44 text
44
How to query data?
● Using LiveData → exposes a stream of events that we can
subscribe to receive updates for asynchronously
● Using RxJava2 (Flowable)
Slide 45
Slide 45 text
45
How to query data?
● Using LiveData → exposes a stream of events that we can
subscribe to receive updates for asynchronously
● Using RxJava2 (Flowable)
● Place synchronous calls in a background thread such as an
AsyncTask. (Room doesn’t allow to issue database queries on the
main thread → ANRs)
Slide 46
Slide 46 text
46
Looking at ViewModels
● ViewModel name comes from MVVM pattern
● Responsible for preparing data for the view
● Expose data to any view listening for changes
● Retain state across activity changes → No onSaveInstanceState()
● Share data across activities/fragments → No actions
● Stay in memory until Lifecycle is scoped
○ Activity finishes / Fragment detached
● Should not reference any View → Memory leak
● Extend AndroidViewModel to access app context
Slide 47
Slide 47 text
47
ViewModel’s Lifecycle
Slide 48
Slide 48 text
48
How to work with AAC?
● Empty Activity project
● Add Google Maven Repo en project’s build.gradle
● Add AAC’s components to app/build.gradle
● Let’s code! :)