# The Evolution of Flow Collection in Jetpack Compose

DevFeed: [The Evolution of Flow Collection in Jetpack Compose](<https://devfeed.tech/articles/the-evolution-of-flow-collection-in-jetpack-compose-85772.md>)

Original publisher: [Read original article](<https://proandroiddev.com/the-evolution-of-flow-collection-in-jetpack-compose-e93416a9d52d?source=rss----c72404660798---4>)

Author: chanzmao

Published: 2026-10-13T00:04:28Z

Content type: article

Language: en

Sources: [ProAndroidDev](<https://devfeed.tech/sources/proandroiddev-medium.md>)

Topics: [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [coroutines-flow](<https://devfeed.tech/topics/coroutines-flow.md>), [Jetpack](<https://devfeed.tech/topics/jetpack.md>), [Boilerplate](<https://devfeed.tech/topics/boilerplate.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [android-architecture](<https://devfeed.tech/tags/android-architecture.md>), [android-compose](<https://devfeed.tech/tags/android-compose.md>), [android-development](<https://devfeed.tech/tags/android-development.md>), [api](<https://devfeed.tech/tags/api.md>), [app-architecture](<https://devfeed.tech/tags/app-architecture.md>), [architecture](<https://devfeed.tech/tags/architecture.md>), [background](<https://devfeed.tech/tags/background.md>), [collection](<https://devfeed.tech/tags/collection.md>), [compose](<https://devfeed.tech/tags/compose.md>), [composition](<https://devfeed.tech/tags/composition.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [evolution](<https://devfeed.tech/tags/evolution.md>), [flow](<https://devfeed.tech/tags/flow.md>), [jetpack](<https://devfeed.tech/tags/jetpack.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [lifecycle](<https://devfeed.tech/tags/lifecycle.md>), [mobile-app-development](<https://devfeed.tech/tags/mobile-app-development.md>), [state](<https://devfeed.tech/tags/state.md>), [ui](<https://devfeed.tech/tags/ui.md>), [upstream](<https://devfeed.tech/tags/upstream.md>), [viewmodel](<https://devfeed.tech/tags/viewmodel.md>)

## AI overview

`collectAsStateWithLifecycle()` packages lifecycle-aware Flow collection into a concise Compose API: collection runs while the lifecycle is active and stops below the chosen state. Earlier options have tradeoffs: `collectAsState()` may keep collecting while the composition remains active; `launchWhenStarted()` can suspend collection while upstream work continues; `repeatOnLifecycle()` cancels and restarts its block but requires more code; and `flowWithLifecycle()` still needs Compose state conversion and an initial value for a regular Flow. Stopping UI collection does not necessarily stop upstream work or refresh data. For example, `stateIn` with `SharingStarted.WhileSubscribed(5_000)` can keep upstream active for five seconds after the last subscriber; later restart behavior depends on the source Flow. A cold HTTP Flow may fetch again, while a Room Flow resuming observation does not necessarily make a network request.

## Source excerpt

From basic state collection to lifecycle-aware UI updates Image created by AI When collecting a Flow in Jetpack Compose, the code can be as simple as this: val uiState by viewModel.uiState.collectAsState() But Android applications have a lifecycle. When an app moves to the background, should the UI continue collecting updates? What happens when the user returns? Over the years, Android developers have used several approaches to handle these questions. Understanding how they evolved helps us choose the right API without adding unnecessary code. Let's walk through the key changes, from collectAsState()to collectAsStateWithLifecycle(). 1. Compose 1.0: collectAsState()July 2021 Jetpack Compose 1.0 introduced collectAsState() as a straightforward way to convert a Flow into Compose State. val uiState by viewModel.uiState.collectAsState() Whenever the Flow emits a new value, Compose updates the state and recomposes the relevant UI. The code is simple, but there is an important distinction: collectAsState() does not automatically stop collection when the Android lifecycle falls below STARTED. For example, when an Activity moves to the background, collection can continue as long as the composition remains active. This isn't necessarily a problem for every Flow. However, if the upstream Flow performs expensive work or maintains an active connection, continuing to collect while the UI is not visible may be undesirable. 2. Lifecycle 2.4.0: launchWhenStarted() and repeatOnLifecycle()October 2021 Before lifecycle-aware collection became a simple Compose API, developers often managed the lifecycle manually. One approach was launchWhenStarted(): lifecycleOwner.lifecycleScope.launch { lifecycleOwner.lifecycle.launchWhenStarted { viewModel.uiState.collect { state -> // Handle state } } } This approach tied execution to the lifecycle, but it had a significant drawback: when the lifecycle stopped, the coroutine could be suspended rather than cancelled. Upstream work could continue unne