Android Architecture
Components
Muhammad Ali
Software Architect
VentureDive
Architecture
• What is architecture
• Why is architecture required?
• How architecture is different from design?
• Popular architectural patterns
• MVC
• MVP
• MVVM
• VIPER
Android Architecture Components
• Google’s official guidelines to architect your Android applications
• Reduce boilerplate
• Separation of concerns
• Testability
• Maintainability
• Ease of Implementation
Android Architecture
Components
• Activities/Fragments
• Represents View Layer
• Handle UI events
• ViewModel
• Observes View lifecycle events
• Bring data from Repository and update
Views
• Uses LiveData to observe changes to
the data in repository
• Repository
• Brings data from sqlite using Room
• Brings data from services
Lifecycle
• Problems without lifecycle
• Sometimes services starts after activity stops, for example a long running
code is being executed in onStart and after that a particular
component/service is started
• In that case component/service is alive when it shouldn’t be
• Lifecycle solves this problem with events and states
• Component can be made lifecycle aware with the help of following
• LifeCycleObserver
• LifeCycleOwner
ViewModel
• View Model is designed to store and manage UI related data in
lifecycle conscious way
• Idea is same, keep Views free from fetching and managing any data
• ViewModel survives configuration changes such as screen resolutions
• ViewModel must not reference a view
• If you need context in a ViewModel, use AndroidViewModel class
ViewModel
LiveData
• LiveData is an observable data holder class
• LiveData is lifecycle aware
• Lifecycle awareness ensures LiveData only update app components
observers that are in active lifecycle state
• Ensure UI is consistent with data
• Sharing resources
• Enduring configuration changes
Room
• Room persistence library provides an abstraction layer over sqlite
• It helps you create a cache of your app’s data on a device running
your app
• This cache acts as a system of record or single source of truth
• Provides an ORM capability with annotations and compile time code
generation
• Remove boilerplate code for creating DAO
Questions?
Thank you

Android architectural components

  • 1.
  • 2.
    Architecture • What isarchitecture • Why is architecture required? • How architecture is different from design? • Popular architectural patterns • MVC • MVP • MVVM • VIPER
  • 3.
    Android Architecture Components •Google’s official guidelines to architect your Android applications • Reduce boilerplate • Separation of concerns • Testability • Maintainability • Ease of Implementation
  • 4.
    Android Architecture Components • Activities/Fragments •Represents View Layer • Handle UI events • ViewModel • Observes View lifecycle events • Bring data from Repository and update Views • Uses LiveData to observe changes to the data in repository • Repository • Brings data from sqlite using Room • Brings data from services
  • 5.
    Lifecycle • Problems withoutlifecycle • Sometimes services starts after activity stops, for example a long running code is being executed in onStart and after that a particular component/service is started • In that case component/service is alive when it shouldn’t be • Lifecycle solves this problem with events and states • Component can be made lifecycle aware with the help of following • LifeCycleObserver • LifeCycleOwner
  • 6.
    ViewModel • View Modelis designed to store and manage UI related data in lifecycle conscious way • Idea is same, keep Views free from fetching and managing any data • ViewModel survives configuration changes such as screen resolutions • ViewModel must not reference a view • If you need context in a ViewModel, use AndroidViewModel class
  • 7.
  • 8.
    LiveData • LiveData isan observable data holder class • LiveData is lifecycle aware • Lifecycle awareness ensures LiveData only update app components observers that are in active lifecycle state • Ensure UI is consistent with data • Sharing resources • Enduring configuration changes
  • 9.
    Room • Room persistencelibrary provides an abstraction layer over sqlite • It helps you create a cache of your app’s data on a device running your app • This cache acts as a system of record or single source of truth • Provides an ORM capability with annotations and compile time code generation • Remove boilerplate code for creating DAO
  • 10.