Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Exploring Android Architecture Components

Exploring Android Architecture Components

Being an Android Developer, we all have faced the common challenges of handling life-cycle events, maintaining state and storing data while developing apps on Android.
Fortunately, the Android Team announced Architecture Components and guidelines to help us solve these common problems and provide an opinionated guide to build Android apps.

In this talk my goal is introduce you to the Architecture Components and guidelines to help you build robust, maintainable and testable apps.

Presented at Mumbai DevFest, 2017.

Akshay Chordiya

October 28, 2017
Tweet

More Decks by Akshay Chordiya

Other Decks in Programming

Transcript

  1. Akshay Chordiya • Co-founder of BitFurther • Android Developer by

    heart • Active community speaker • Co-author of upcoming book “Kotlin Blueprints” • Graduated in 2016 @Aky @Akshay_Chordiya AkshayChordiya
  2. Agenda • Life before • Why Architecture Components? • What

    are these components? • Exploring the arch components • Guide to App Architecture • Pro-Tips • Conclusion • Q&A
  3. Life before • Android Development is difficult!* • No official

    opinionated guide • Confusion deciding architecture • Under and over architecturing problem • The architecture war (MVP, MVVM, Clean, etc)
  4. The workaround override fun onCreate(savedInstanceState: Bundle?) { /// .... //

    Network call request = api.get() request.onResponse { data -> textView.text = data request = null } } override fun onDestory() { if (request != null) request.cancel() } BORING Boilerplate
  5. Anatomy of Lifecycle LifecycleOwners are objects with Lifecycle like Activity

    and Fragment. LifecycleObservers observe LifecycleOwners, and are notified about lifecycle changes LifecycleOwner (Activity/Fragment) LifecycleObserver (Ex. LiveData)
  6. What is LiveData? • An observable data holder • It

    notifies the observers when data changes so that you can update the UI • Respects lifecycle aka it is lifecycle aware • Pretty similar to RxJava
  7. How it works? Observes for updates LiveData value updated Is

    STARTED or RESUMED? Notify the observer Update the UI
  8. Advantages of LiveData • Always up to date data •

    It is lifecycle aware • Handles proper configuration change • No memory leaks
  9. What is ViewModel? • Designed to hold and manage UI

    related data • Survives configuration changes • It is lifecycle aware • It will often store LiveData objects • Helps to communicate between Activity and Fragment • Eliminates “God Activities”
  10. Code: ViewModel class NewsViewModel() : ViewModel() { private var newsArticles:

    List<NewsArticles> init { // Load the data over here newsArticles = .... } fun getNewsArticles(): List<NewsArticles> { return newsArticles } }
  11. Code: ViewModel class NewsViewModel() : ViewModel() { private var newsArticles:

    LiveData<List<NewsArticles>> init { // Load the data over here newsArticles = .... } fun getNewsArticles(): LiveData<List<NewsArticles>> { return newsArticles } }
  12. Usage: ViewModel // Get ViewModel val newsViewModel = ViewModelProviders.of(this).get(NewsViewModel::class.java) //

    Observing for data change newsViewModel.getNewsArticles().observe(this, Observer<List<NewsArticles>> { // Update the UI })
  13. What is Room? • It provides local data persistence •

    Abstraction layer over existing SQLite database • Checks SQL query at compile time • Ability to observe for changes in the database using LiveData • Eliminates the boilerplate code • Plays well with RxJava
  14. Entity @Entity(tableName = "news_article") data class NewsArticles( @PrimaryKey(autoGenerate = true)

    var id: Int = 0, var author: String? = null, var title: String = "", var description: String = "")
  15. DAO @Dao interface NewsArticlesDao { @Insert fun insertArticle(article: NewsArticles) @Query("SELECT

    * FROM news_article") fun getNewsArticles(): LiveData<List<NewsArticles>> @Update fun updateArticle(article: Article) @Delete fun deleteArticle(article: Article) } CRUD operations Compile time verification
  16. Database @Database(entities = arrayOf(NewsArticles::class), version = 1) abstract class NewsDatabase

    : RoomDatabase() { /** * Get news article DAO */ abstract fun newsArticlesDao(): NewsArticlesDao }
  17. Anatomy of Room • Defines database holder • Main access

    point to the database connection • Main component of Room • It defines the methods for interaction with the database • Implementation is auto-generated • For each entity a database table is created • It represents a class which holds a database row
  18. Testing Component Tests Mock UI Espresso ViewModel ViewModel JUnit Repository

    Repository JUnit DAO and WebService DAO Instrumented - WebService Instrumented MockWebServer
  19. Pro-Tips • You can use them in production! • Mix

    match them to solve your problem • Don’t shift if you're already happy with existing architecture • Use in-memory database mode for Room when developing or testing • Use LifecycleService to get life-cycle in Service
  20. Further Resources • All the images and logos used are

    trademarks of respective companies and/or author • Android Architecture • Guide to App Architecture • Introduction to Architecture Components - Medium • Exploring ViewModel Architecture component • Exploring LiveData Architecture component • Exploring Room Architecture component • Sample news app with Architecture Components • Architecture Components: Improve Your App's Design • Google I/O Architecture Components Talk • Architecture Components - GDD • Getting started with Kotlin - YouTube