# collectAsStateWithLifecycle

Common/All

## Functions summary

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State<T>` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<T>.https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/collectAsStateWithLifecycle.composable#(kotlinx.coroutines.flow.Flow).collectAsStateWithLifecycle(kotlin.Any,androidx.lifecycle.Lifecycle,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)( initialValue: T, lifecycle: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle, minActiveState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State, context: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html )` Collects values from this `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` and represents its latest value via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` in a lifecycle-aware manner. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State<T>` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<T>.https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/collectAsStateWithLifecycle.composable#(kotlinx.coroutines.flow.Flow).collectAsStateWithLifecycle(kotlin.Any,androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)( initialValue: T, lifecycleOwner: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner, minActiveState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State, context: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html )` Collects values from this `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` and represents its latest value via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` in a lifecycle-aware manner. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State<T>` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html<T>.https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/collectAsStateWithLifecycle.composable#(kotlinx.coroutines.flow.StateFlow).collectAsStateWithLifecycle(androidx.lifecycle.Lifecycle,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)( lifecycle: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle, minActiveState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State, context: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html )` Collects values from this `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` and represents its latest value via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` in a lifecycle-aware manner. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State<T>` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html<T>.https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/collectAsStateWithLifecycle.composable#(kotlinx.coroutines.flow.StateFlow).collectAsStateWithLifecycle(androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)( lifecycleOwner: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner, minActiveState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State, context: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html )` Collects values from this `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` and represents its latest value via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` in a lifecycle-aware manner. | Cmn |

## Functions

### Flow.collectAsStateWithLifecycle

Cmn Artifact: [androidx.lifecycle:lifecycle-runtime-compose](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/compose/FlowExt.kt+function:collectAsStateWithLifecycle) Added in [2.10.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.10.0)

```
@Composable
fun <T : Any?> Flow<T>.collectAsStateWithLifecycle(
    initialValue: T,
    lifecycle: Lifecycle,
    minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
    context: CoroutineContext = EmptyCoroutineContext
): State<T>
```

Collects values from this `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` and represents its latest value via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` in a lifecycle-aware manner.

Every time there would be new value posted into the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` the returned `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` will be updated causing recomposition of every `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State#value()` usage whenever the `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.Flow).collectAsStateWithLifecycle(kotlin.Any,androidx.lifecycle.Lifecycle,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)` is at least `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.Flow).collectAsStateWithLifecycle(kotlin.Any,androidx.lifecycle.Lifecycle,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)`.

This `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` is collected every time `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.Flow).collectAsStateWithLifecycle(kotlin.Any,androidx.lifecycle.Lifecycle,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)` reaches the `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.Flow).collectAsStateWithLifecycle(kotlin.Any,androidx.lifecycle.Lifecycle,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)` Lifecycle state. The collection stops when `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.Flow).collectAsStateWithLifecycle(kotlin.Any,androidx.lifecycle.Lifecycle,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)` falls below `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.Flow).collectAsStateWithLifecycle(kotlin.Any,androidx.lifecycle.Lifecycle,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)`.

```kotlin
import androidx.compose.material.Text
import androidx.compose.runtime.remember
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.collectAsStateWithLifecycle

class ExampleState {
    val counter = flow {
        var count = 0
        while (true) {
            emit(count++)
            delay(1000)
        }
    }
}

val state = remember { ExampleState() }
val count by state.counter.collectAsStateWithLifecycle(initialValue = 0)
Text(text = "$count")
```

Warning: `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#INITIALIZED` is not allowed in this API. Passing it as a parameter will throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`.

| Parameters |
|---|---|
| `initialValue: T` | The initial value given to the returned `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State#value()`. |
| `lifecycle: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` used to restart collecting `this` flow. |
| `minActiveState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State = Lifecycle.State.STARTED` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` in which the upstream flow gets collected. The collection will stop if the lifecycle falls below that state, and will restart if it's in that state again. |
| `context: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html = EmptyCoroutineContext` | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` to use for collecting. |

### Flow.collectAsStateWithLifecycle

Cmn Artifact: [androidx.lifecycle:lifecycle-runtime-compose](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/compose/FlowExt.kt+function:collectAsStateWithLifecycle) Added in [2.10.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.10.0)

```
@Composable
fun <T : Any?> Flow<T>.collectAsStateWithLifecycle(
    initialValue: T,
    lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current,
    minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
    context: CoroutineContext = EmptyCoroutineContext
): State<T>
```

Collects values from this `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` and represents its latest value via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` in a lifecycle-aware manner.

Every time there would be new value posted into the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` the returned `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` will be updated causing recomposition of every `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State#value()` usage whenever the `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.Flow).collectAsStateWithLifecycle(kotlin.Any,androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)`'s lifecycle is at least `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.Flow).collectAsStateWithLifecycle(kotlin.Any,androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)`.

This `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` is collected every time the `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.Flow).collectAsStateWithLifecycle(kotlin.Any,androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)`'s lifecycle reaches the `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.Flow).collectAsStateWithLifecycle(kotlin.Any,androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)` Lifecycle state. The collection stops when the `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.Flow).collectAsStateWithLifecycle(kotlin.Any,androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)`'s lifecycle falls below `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.Flow).collectAsStateWithLifecycle(kotlin.Any,androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)`.

```kotlin
import androidx.compose.material.Text
import androidx.compose.runtime.remember
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.collectAsStateWithLifecycle

class ExampleState {
    val counter = flow {
        var count = 0
        while (true) {
            emit(count++)
            delay(1000)
        }
    }
}

val state = remember { ExampleState() }
val count by state.counter.collectAsStateWithLifecycle(initialValue = 0)
Text(text = "$count")
```

Warning: `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#INITIALIZED` is not allowed in this API. Passing it as a parameter will throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`.

| Parameters |
|---|---|
| `initialValue: T` | The initial value given to the returned `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State#value()`. |
| `lifecycleOwner: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner = LocalLifecycleOwner.current` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` whose `lifecycle` is used to restart collecting `this` flow. |
| `minActiveState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State = Lifecycle.State.STARTED` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` in which the upstream flow gets collected. The collection will stop if the lifecycle falls below that state, and will restart if it's in that state again. |
| `context: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html = EmptyCoroutineContext` | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` to use for collecting. |

### StateFlow.collectAsStateWithLifecycle

Cmn Artifact: [androidx.lifecycle:lifecycle-runtime-compose](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/compose/FlowExt.kt+function:collectAsStateWithLifecycle) Added in [2.10.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.10.0)

```
@Composable
fun <T : Any?> StateFlow<T>.collectAsStateWithLifecycle(
    lifecycle: Lifecycle,
    minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
    context: CoroutineContext = EmptyCoroutineContext
): State<T>
```

Collects values from this `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` and represents its latest value via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` in a lifecycle-aware manner.

The `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/value.html` is used as an initial value. Every time there would be new value posted into the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` the returned `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` will be updated causing recomposition of every `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State#value()` usage whenever the `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsStateWithLifecycle(androidx.lifecycle.Lifecycle,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)` is at least `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsStateWithLifecycle(androidx.lifecycle.Lifecycle,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)`.

This `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` is collected every time `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsStateWithLifecycle(androidx.lifecycle.Lifecycle,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)` reaches the `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsStateWithLifecycle(androidx.lifecycle.Lifecycle,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)` Lifecycle state. The collection stops when `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsStateWithLifecycle(androidx.lifecycle.Lifecycle,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)` falls below `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsStateWithLifecycle(androidx.lifecycle.Lifecycle,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)`.

```kotlin
import androidx.compose.material.Text
import androidx.compose.runtime.remember
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.collectAsStateWithLifecycle

class ExampleState {
    private val _uiState = MutableStateFlow("")
    val uiState: StateFlow<String> = _uiState.asStateFlow()
}

val state = remember { ExampleState() }

val uiState by state.uiState.collectAsStateWithLifecycle()
Text(text = uiState)
```

Warning: `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#INITIALIZED` is not allowed in this API. Passing it as a parameter will throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`.

| Parameters |
|---|---|
| `lifecycle: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` used to restart collecting `this` flow. |
| `minActiveState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State = Lifecycle.State.STARTED` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` in which the upstream flow gets collected. The collection will stop if the lifecycle falls below that state, and will restart if it's in that state again. |
| `context: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html = EmptyCoroutineContext` | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` to use for collecting. |

### StateFlow.collectAsStateWithLifecycle

Cmn Artifact: [androidx.lifecycle:lifecycle-runtime-compose](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/compose/FlowExt.kt+function:collectAsStateWithLifecycle) Added in [2.10.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.10.0)

```
@Composable
fun <T : Any?> StateFlow<T>.collectAsStateWithLifecycle(
    lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current,
    minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
    context: CoroutineContext = EmptyCoroutineContext
): State<T>
```

Collects values from this `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` and represents its latest value via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` in a lifecycle-aware manner.

The `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/value.html` is used as an initial value. Every time there would be new value posted into the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` the returned `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` will be updated causing recomposition of every `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State#value()` usage whenever the `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsStateWithLifecycle(androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)`'s lifecycle is at least `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsStateWithLifecycle(androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)`.

This `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` is collected every time the `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsStateWithLifecycle(androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)`'s lifecycle reaches the `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsStateWithLifecycle(androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)` Lifecycle state. The collection stops when the `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsStateWithLifecycle(androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)`'s lifecycle falls below `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsStateWithLifecycle(androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)`.

```kotlin
import androidx.compose.material.Text
import androidx.compose.runtime.remember
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.collectAsStateWithLifecycle

class ExampleState {
    private val _uiState = MutableStateFlow("")
    val uiState: StateFlow<String> = _uiState.asStateFlow()
}

val state = remember { ExampleState() }

val uiState by state.uiState.collectAsStateWithLifecycle()
Text(text = uiState)
```

Warning: `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#INITIALIZED` is not allowed in this API. Passing it as a parameter will throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`.

| Parameters |
|---|---|
| `lifecycleOwner: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner = LocalLifecycleOwner.current` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` whose `lifecycle` is used to restart collecting `this` flow. |
| `minActiveState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State = Lifecycle.State.STARTED` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` in which the upstream flow gets collected. The collection will stop if the lifecycle falls below that state, and will restart if it's in that state again. |
| `context: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html = EmptyCoroutineContext` | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` to use for collecting. |