
LiveData with Coroutines and Flow — Part I: Reactive UIs
This article is a summary of the talk I gave with Yigit Boyar at the Android Dev Summit 2019.
Summary
The article discusses integrating LiveData with coroutines for reactive UI development in Android, as presented at the Android Dev Summit 2019.
Abstract
The article provides a comprehensive overview of using LiveData in conjunction with Kotlin coroutines to create reactive UIs in Android applications. It emphasizes the importance of a layered architecture to manage complex lifecycles and recommends the use of ViewModels and the observer pattern to decouple UI from business logic. The author, who spoke at the Android Dev Summit 2019 with Yigit Boyar, outlines the benefits of the Android Architecture Components ViewModel for handling configuration changes and simplifying lifecycle management. The article also explores the concept of operation scopes, suggesting that certain operations are better suited to the lifecycle of a ViewModel or an Activity, and introduces Kotlin Coroutines as a solution for managing concurrency in Android applications.
Opinions

This article is a summary of the talk I gave with Yigit Boyar at the Android Dev Summit 2019.
Part I: Reactive UIs (this post)
Part II: Launching coroutines with Architecture Components
Part III: LiveData and Coroutines patterns
Since the early days of Android, we’ve learned very quickly that Android lifecycles were hard to understand, full of edge cases, and that the best way to stay sane was to avoid them as much as possible.
For this, we recommend a layered architecture, so we can write UI-independent code without thinking too much about lifecycles. For example, we can add a domain layer that holds business logic (what you app actually does) and a data layer.
Furthermore, we learned that the presentation layer could be split into different components with different responsibilities:
Naming aside, there are two mechanisms to send data from the ViewModel/Presenter to the View:
This is a convention that is pretty well established in the Android community, but you’ll find articles that disagree. There are hundreds of blog posts defining Presenter, ViewModel, MVP and MVVM in different ways. My suggestion is that you focus on the characteristics of your presentation layer, and that you use the Android Architecture Components ViewModel which:
The ViewModel is designed to be used using the observer pattern:
When a View (an Activity, Fragment or any Lifecycle owner) is created, the ViewModel is obtained and it starts exposing data through one or more LiveDatas, which the View subscribes to.
This subscription can be set up with LiveData.observe or automatically with the Data Binding library.
Now, if the device is rotated then the View is destroyed (#1) and a new instance is created (#2):
If we had a reference to the activity in the ViewModel, we would need to make sure to:
But we don’t have to deal with this anymore with ViewModel+LiveData. This is why we recommend this approach in the Guide to App Architecture.
As Activities and Fragments have an equal-or-shorter lifespan than ViewModels, we can start talking about the scope of operations.
An operation is anything you need to do in your app, like fetching data from the network, filtering results or calculating the arrangement of some text.
For any operation that you create, you need to think about its scope: the extent of time between launch and when it’s cancelled. Let’s look at two examples:
onStart and you stop it in onStop.initblock and you stop it in onCleared().
Looking at the diagram, we can locate where each operation makes sense.
Obviously, a real world app can have a lot more scopes than these. For example, in the Android Dev Summit App we can use:

This can produce a dozen different scopes so managing all can get overwhelming. We need a way to structure this concurrency!
One very convenient solution is Kotlin Coroutines.
We love using Coroutines in Android for many reasons. Some of them are:
If you want an intro to coroutines, check out Android’s introduction and Kotlin’s official documentation.
On to Part II: Launching coroutines with Architecture Components
Let’s learn the right way of collecting Flows inside Activities and Fragments.
Shinoo GoyalMutableSharedFlow has 3 parameters and there are default values for it. They have their own significance and the SharedFlow behaviour…
Nine Pages Of My LifeThe world of Android mobile apps is a vibrant ecosystem. For aspiring developers, the path to becoming a senior Android developer is an…
Christophe BeylsMaking timers lifecycle-aware