# Consuming flows safely in Jetpack Compose

DevFeed: [Consuming flows safely in Jetpack Compose](<https://devfeed.tech/articles/consuming-flows-safely-in-jetpack-compose-35672.md>)

Original publisher: [Read original article](<https://manuelvivo.dev/consuming-flows-compose>)

Author: Manuel Vivo

Published: 2022-08-10T00:00:00Z

Content type: tutorial

Language: en

Sources: [Manuel Vivo .dev](<https://devfeed.tech/sources/manuel-vivo-dev.md>)

Topics: [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [API](<https://devfeed.tech/topics/api.md>), [Android](<https://devfeed.tech/topics/android.md>), [ui](<https://devfeed.tech/topics/ui.md>), [function](<https://devfeed.tech/topics/function.md>), [implementation](<https://devfeed.tech/topics/implementation.md>), [Boilerplate](<https://devfeed.tech/topics/boilerplate.md>), [now-in-android](<https://devfeed.tech/topics/now-in-android.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [architecture](<https://devfeed.tech/tags/architecture.md>), [boilerplate](<https://devfeed.tech/tags/boilerplate.md>), [compose](<https://devfeed.tech/tags/compose.md>), [flow](<https://devfeed.tech/tags/flow.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [jetpack](<https://devfeed.tech/tags/jetpack.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [lifecycle](<https://devfeed.tech/tags/lifecycle.md>), [now-in-android](<https://devfeed.tech/tags/now-in-android.md>), [stateflow](<https://devfeed.tech/tags/stateflow.md>), [ui](<https://devfeed.tech/tags/ui.md>), [viewmodel](<https://devfeed.tech/tags/viewmodel.md>)

## AI overview

This tutorial explains how to use collectAsStateWithLifecycle in Jetpack Compose to collect Flow and StateFlow values from UI code in a lifecycle-aware way. It describes how collection follows the app lifecycle, stops by default when the app is in the background, and helps avoid unnecessary resource usage. The API is identified as alpha and requires opting in to its annotation.

## Source excerpt

Use the collectAsStateWithLifecycle API to collect flows in a lifecycle-aware manner from your UI.