Using Flow on Android
In this section, you will look into using Flows for asynchronous programming in Android. Flow, an asynchronous stream library built on top of Kotlin Coroutines, is ideal for live data updates in your application. Android Jetpack libraries include Room, WorkManager, and Jetpack Compose, and third-party libraries support Flow.
A Flow of data is represented by the kotlinx.coroutines.flow.Flow
interface. Flows emit multiple values of the same type one at a time. For example, Flow<String>
is a Flow that emits string values.
A flow starts to emit values when you call the suspending collect
function from a coroutine or another suspending function. In the following example, the collect
function was called from the coroutine created using the launch
builder of lifecycleScope
:
class MainActivity : AppCompatActivity() { ... override fun onCreate(savedInstanceState: Bundle?) { ...