# Lifecycle

Artifact: [androidx.lifecycle:lifecycle-common](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/Lifecycle.kt+class:androidx.lifecycle.Lifecycle) Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

*** ** * ** ***

Common/AllAndroid/JVM
- Cmn

  ```
  abstract class Lifecycle
  ```
Known direct subclasses [LifecycleRegistry](https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleRegistry)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleRegistry` | A `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` implementation that manages multiple `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleObserver`s. |

*** ** * ** ***

Defines an object with a lifecycle state control flow.

Commonly implemented by UI container classes (such as Activities and Fragments on Android) or custom components to expose their lifecycle to other components.

`https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event#ON_CREATE`, `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event#ON_START`, `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event#ON_RESUME` events are dispatched **after** the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner`'s related method returns. `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event#ON_PAUSE`, `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event#ON_STOP`, `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event#ON_DESTROY` events are dispatched **before** the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner`'s related method is called. This gives you certain guarantees on which state the owner is in.

To observe lifecycle events, call `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#addObserver(androidx.lifecycle.LifecycleObserver)` passing an object that implements either `https://developer.android.com/reference/kotlin/androidx/lifecycle/DefaultLifecycleObserver` or `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleEventObserver`.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` | for the valid lifecycle states. |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event` | for the transition events between states. |

## Summary

| ### Nested types |
|---|
| `enum https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-enum/index.html<https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event>` Represents a transition event triggered by a change in the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner`'s state. |
| `enum https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-enum/index.html<https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State>` Represents the current lifecycle state of a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner`. |

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#Lifecycle()()` | Cmn |

| ### Public functions |
|---|---|---|
| `abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#addObserver(androidx.lifecycle.LifecycleObserver)(observer: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleObserver)` Adds a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleObserver` to receive `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` state changes. | Cmn |
| `abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#removeObserver(androidx.lifecycle.LifecycleObserver)(observer: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleObserver)` Removes the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#removeObserver(androidx.lifecycle.LifecycleObserver)` from the list of registered observers. | Cmn |

| ### Public properties |
|---|---|---|
| `abstract https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#currentState()` The current `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` of the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle`. | Cmn |
| `open https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html<https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State>` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#currentStateFlow()` Returns a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` where the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/value.html` represents the current `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` of this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle`. | Cmn |

| ### Extension functions |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State<https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State>` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).currentStateAsState()()` Collects values from the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#currentStateFlow()` and represents its latest value via `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State`. | Cmn |
| `inline https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleObserver` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).addObserver(kotlin.Function3)( crossinline action: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleObserver.(source: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner, event: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` Adds a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleObserver` to this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` using the provided `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).addObserver(kotlin.Function3)`. | Cmn |
| `suspend T` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.[whenCreated](https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).whenCreated(kotlin.coroutines.SuspendFunction1))(block: suspend https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html.() -> T)` **This function is deprecated.** whenCreated has been deprecated because it runs the block on a pausing dispatcher that suspends, rather than cancels work when the lifecycle state goes below the given state. | android |
| `suspend T` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.[whenResumed](https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).whenResumed(kotlin.coroutines.SuspendFunction1))(block: suspend https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html.() -> T)` **This function is deprecated.** whenResumed has been deprecated because it runs the block on a pausing dispatcher that suspends, rather than cancels work when the lifecycle state goes below the given state. | android |
| `suspend T` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.[whenStarted](https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).whenStarted(kotlin.coroutines.SuspendFunction1))(block: suspend https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html.() -> T)` **This function is deprecated.** whenStarted has been deprecated because it runs the block on a pausing dispatcher that suspends, rather than cancels work when the lifecycle state goes below the given state. | android |
| `suspend T` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.[whenStateAtLeast](https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1))( minState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State, block: suspend https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html.() -> T )` **This function is deprecated.** whenStateAtLeast has been deprecated because it runs the block on a pausing dispatcher that suspends, rather than cancels work when the lifecycle state goes below the given state. | android |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)( state: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State, block: suspend https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` Runs the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` in a new coroutine when `this` `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` is at least at `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` and suspends the execution until `this` `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` is `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED`. | Cmn |
| `suspend inline R` | `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).withCreated(kotlin.Function0)(crossinline block: () -> R)` Run `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).withCreated(kotlin.Function0)` with this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` in a `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` of at least `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#CREATED` and resume with the result. | Cmn |
| `suspend inline R` | `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).withResumed(kotlin.Function0)(crossinline block: () -> R)` Run `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).withResumed(kotlin.Function0)` with this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` in a `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` of at least `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#RESUMED` and resume with the result. | Cmn |
| `suspend inline R` | `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).withStarted(kotlin.Function0)(crossinline block: () -> R)` Run `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).withStarted(kotlin.Function0)` with this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` in a `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` of at least `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#STARTED` and resume with the result. | Cmn |
| `suspend inline R` | `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).withStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.Function0)( state: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State, crossinline block: () -> R )` Run `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).withStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.Function0)` with this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` in a `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` of at least `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).withStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.Function0)` and resume with the result. | Cmn |

| ### Extension properties |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleCoroutineScope` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).coroutineScope()` `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` tied to this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle`. | Cmn |
| `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event>` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).eventFlow()` Creates a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` of `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event`s dispatched by this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle`. | Cmn |

## Public constructors

### Lifecycle

Cmn Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
Lifecycle()
```

## Public functions

### addObserver

Cmn Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
@MainThread
abstract fun addObserver(observer: LifecycleObserver): Unit
```

Adds a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleObserver` to receive `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` state changes.

Brings the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#addObserver(androidx.lifecycle.LifecycleObserver)` up to the current `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` of the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner`. For example, if the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` is in `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#STARTED`, the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#addObserver(androidx.lifecycle.LifecycleObserver)` receives `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event#ON_CREATE` and `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event`s.

| Parameters |
|---|---|
| `observer: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleObserver` | The observer to notify. |

### removeObserver

Cmn Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
@MainThread
abstract fun removeObserver(observer: LifecycleObserver): Unit
```

Removes the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#removeObserver(androidx.lifecycle.LifecycleObserver)` from the list of registered observers.

If called while a state change is being dispatched:

- If the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#removeObserver(androidx.lifecycle.LifecycleObserver)` has not yet received that event, it will not receive it.

- If the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#removeObserver(androidx.lifecycle.LifecycleObserver)` has more than one method that observes the currently dispatched event, and at least one of them received the event, all of them receive it, and removal occurs afterward.

| Parameters |
|---|---|
| `observer: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleObserver` | The observer to remove. |

## Public properties

### currentState

Cmn Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
abstract val currentState: Lifecycle.State
```

The current `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` of the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle`.

### currentStateFlow

Cmn Added in [2.7.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.7.0)

```
open val currentStateFlow: StateFlow<Lifecycle.State>
```

Returns a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` where the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/value.html` represents the current `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` of this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle`.

## Extension functions

### Lifecycle.currentStateAsState

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/LifecycleExt.kt+function:currentStateAsState)

```
@Composable
fun Lifecycle.currentStateAsState(): State<Lifecycle.State>
```

Collects values from the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#currentStateFlow()` and represents its latest value via `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State`. 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/lifecycle/Lifecycle.State` will be updated causing recomposition of every `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State#value()` usage.

### Lifecycle.addObserver

Cmn Artifact: [androidx.lifecycle:lifecycle-common](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/Lifecycle.kt+function:addObserver)

```
inline fun Lifecycle.addObserver(
    crossinline action: LifecycleObserver.(source: LifecycleOwner, event: Lifecycle.Event) -> Unit
): LifecycleObserver
```

Adds a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleObserver` to this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` using the provided `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).addObserver(kotlin.Function3)`.

Invokes `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).addObserver(kotlin.Function3)` whenever a `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event` occurs.

| Parameters |
|---|---|
| `crossinline action: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleObserver.(source: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner, event: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | The action invoked on each `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event`, providing the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` and the specific `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event`. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleObserver` | the added `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleObserver` instance (can be used to later remove it). |

### Lifecycle.whenCreated

android Artifact: [androidx.lifecycle:lifecycle-common](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/PausingDispatcher.jvm.kt+function:whenCreated)

```
suspend fun <T : Any?> Lifecycle.[whenCreated](https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).whenCreated(kotlin.coroutines.SuspendFunction1))(block: suspend CoroutineScope.() -> T): T
```

> [!CAUTION]
> **This function is deprecated.**   
> whenCreated has been deprecated because it runs the block on a pausing dispatcher that suspends, rather than cancels work when the lifecycle state goes below the given state. Use withCreated for non-suspending work that needs to run only once when the Lifecycle changes.

Runs the given block when the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` is at least in `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#CREATED` state.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` | for details |

### Lifecycle.whenResumed

android Artifact: [androidx.lifecycle:lifecycle-common](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/PausingDispatcher.jvm.kt+function:whenResumed)

```
suspend fun <T : Any?> Lifecycle.[whenResumed](https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).whenResumed(kotlin.coroutines.SuspendFunction1))(block: suspend CoroutineScope.() -> T): T
```

> [!CAUTION]
> **This function is deprecated.**   
> whenResumed has been deprecated because it runs the block on a pausing dispatcher that suspends, rather than cancels work when the lifecycle state goes below the given state. Use withResumed for non-suspending work that needs to run only once when the Lifecycle changes.

Runs the given block when the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` is at least in `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#RESUMED` state.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` | for details |

### Lifecycle.whenStarted

android Artifact: [androidx.lifecycle:lifecycle-common](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/PausingDispatcher.jvm.kt+function:whenStarted)

```
suspend fun <T : Any?> Lifecycle.[whenStarted](https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).whenStarted(kotlin.coroutines.SuspendFunction1))(block: suspend CoroutineScope.() -> T): T
```

> [!CAUTION]
> **This function is deprecated.**   
> whenStarted has been deprecated because it runs the block on a pausing dispatcher that suspends, rather than cancels work when the lifecycle state goes below the given state. Use withStarted for non-suspending work that needs to run only once when the Lifecycle changes.

Runs the given block when the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` is at least in `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#STARTED` state.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` | for details |

### Lifecycle.whenStateAtLeast

android Artifact: [androidx.lifecycle:lifecycle-common](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/PausingDispatcher.jvm.kt+function:whenStateAtLeast)

```
suspend fun <T : Any?> Lifecycle.[whenStateAtLeast](https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1))(
    minState: Lifecycle.State,
    block: suspend CoroutineScope.() -> T
): T
```

> [!CAUTION]
> **This function is deprecated.**   
> whenStateAtLeast has been deprecated because it runs the block on a pausing dispatcher that suspends, rather than cancels work when the lifecycle state goes below the given state. Use withStateAtLeast for non-suspending work that needs to run only once when the Lifecycle changes.

Runs the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` on a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/index.html` that executes the `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` on the main thread and suspends the execution unless the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle`'s state is at least `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)`.

If the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` moves to a lesser state while the `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` is running, the `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` will be suspended until the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` reaches to a state greater or equal to `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)`.

Note that this won't effect any sub coroutine if they use a different `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/index.html`. However, the `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` will not resume execution when the sub coroutine finishes unless the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` is at least in `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)`.

If the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` is destroyed while the `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` is suspended, the `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` will be cancelled which will also cancel any child coroutine launched inside the `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)`.

If you have a `try finally` block in your code, the `finally` might run after the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` moves outside the desired state. It is recommended to check the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#currentState()` before accessing the UI. Similarly, if you have a `catch` statement that might catch `CancellationException`, you should check the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#currentState()` before accessing the UI. See the sample below for more details.

```kotlin
// running a block of code only if lifecycle is STARTED
viewLifecycle.whenStateAtLeast(Lifecycle.State.STARTED) {
    // here, we are on the main thread and view lifecycle is guaranteed to be STARTED or RESUMED.
    // We can safely access our views.
    loadingBar.visibility = View.VISIBLE
    try {
        // we can call any suspend function
        val data = withContext(Dispatchers.IO) {
            // this will run in IO thread pool. It will keep running as long as Lifecycle
            // is not DESTROYED. If it is destroyed, this coroutine will be cancelled as well.
            // However, we CANNOT access Views here.

            // We are using withContext(Dispatchers.IO) here just for demonstration purposes.
            // Such code should live in your business logic classes and your UI should use a
            // ViewModel (or similar) to access it.
            api.getUser()
        }
        // this line will execute on the main thread and only if the lifecycle is in at least
        // STARTED state (STARTED is the parameter we've passed to whenStateAtLeast)
        // Because of this guarantee, we can safely access the UI again.
        loadingBar.visibility = View.GONE
        nameTextView.text = user.name
        lastNameTextView.text = user.lastName
    } catch(ex : UserNotFoundException) {
        // same as above, this code can safely access UI elements because it only runs if
        // view lifecycle is at least STARTED
        loadingBar.visibility = View.GONE
        showErrorDialog(ex)
    } catch(th : Throwable) {
         // Unlike the catch statement above, this catch statements it too generic and might
         // also catch the CancellationException. Before accessing UI, you should check isActive
         // or lifecycle state
         if (viewLifecycle.currentState >= Lifecycle.State.STARTED) {
             // here you can access the view because you've checked the coroutine is active
         }
    } finally {
        // in case of cancellation, this line might run even if the Lifecycle is not DESTROYED.
        // You cannot access Views here unless you check `isActive` or lifecycle state
        if (viewLifecycle.currentState >= Lifecycle.State.STARTED) {
            // safe to access views
        } else {
            // not safe to access views
        }
    }
}
```

| Parameters |
|---|---|
| `minState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` | The desired minimum state to run the `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)`. |
| `block: suspend https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html.() -> T` | The block to run when the lifecycle is at least in `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)`. |

| Returns |
|---|---|
| `T` | The return value of the \[block\] |

### Lifecycle.repeatOnLifecycle

Cmn Artifact: [androidx.lifecycle:lifecycle-runtime](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/RepeatOnLifecycle.kt+function:repeatOnLifecycle)

```
suspend fun Lifecycle.repeatOnLifecycle(
    state: Lifecycle.State,
    block: suspend CoroutineScope.() -> Unit
): Unit
```

Runs the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` in a new coroutine when `this` `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` is at least at `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` and suspends the execution until `this` `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` is `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED`.

The `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` will cancel and re-launch as the lifecycle moves in and out of the target state.

```kotlin
class MyActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        /* ... */
        // Runs the block of code in a coroutine when the lifecycle is at least STARTED.
        // The coroutine will be cancelled when the ON_STOP event happens and will
        // restart executing if the lifecycle receives the ON_START event again.
        lifecycleScope.launch {
            lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                uiStateFlow.collect { uiState ->
                    updateUi(uiState)
                }
            }
        }
    }
}
```

The best practice is to call this function when the lifecycle is initialized. For example, `onCreate` in an Activity, or `onViewCreated` in a Fragment. Otherwise, multiple repeating coroutines doing the same could be created and be executed at the same time.

Repeated invocations of `block` will run serially, that is they will always wait for the previous invocation to fully finish before re-starting execution as the state moves in and out of the required state.

**Thread Safety** : This function always executes the lifecycle registration and synchronization on `Dispatchers.Main.immediate`. If the caller coroutine context runs on a different dispatcher, it yields to the main dispatcher. The target `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` also executes on the main dispatcher.

**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 |
|---|---|
| `state: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` in which `block` runs in a new coroutine. That coroutine will cancel if the lifecycle falls below that state, and will restart if it's in that state again. |
| `block: suspend https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | The block to run when the lifecycle is at least in `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` state. |

### Lifecycle.withCreated

Cmn Artifact: [androidx.lifecycle:lifecycle-runtime](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/WithLifecycleState.kt+function:withCreated)

```
suspend inline fun <R : Any?> Lifecycle.withCreated(crossinline block: () -> R): R
```

Run `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).withCreated(kotlin.Function0)` with this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` in a `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` of at least `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#CREATED` and resume with the result. Throws the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleDestroyedException` if the lifecycle has reached `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` by the time of the call or before `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).withCreated(kotlin.Function0)` is able to run.

### Lifecycle.withResumed

Cmn Artifact: [androidx.lifecycle:lifecycle-runtime](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/WithLifecycleState.kt+function:withResumed)

```
suspend inline fun <R : Any?> Lifecycle.withResumed(crossinline block: () -> R): R
```

Run `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).withResumed(kotlin.Function0)` with this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` in a `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` of at least `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#RESUMED` and resume with the result. Throws the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleDestroyedException` if the lifecycle has reached `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` by the time of the call or before `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).withResumed(kotlin.Function0)` is able to run.

### Lifecycle.withStarted

Cmn Artifact: [androidx.lifecycle:lifecycle-runtime](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/WithLifecycleState.kt+function:withStarted)

```
suspend inline fun <R : Any?> Lifecycle.withStarted(crossinline block: () -> R): R
```

Run `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).withStarted(kotlin.Function0)` with this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` in a `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` of at least `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#STARTED` and resume with the result. Throws the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleDestroyedException` if the lifecycle has reached `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` by the time of the call or before `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).withStarted(kotlin.Function0)` is able to run.

### Lifecycle.withStateAtLeast

Cmn Artifact: [androidx.lifecycle:lifecycle-runtime](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/WithLifecycleState.kt+function:withStateAtLeast)

```
suspend inline fun <R : Any?> Lifecycle.withStateAtLeast(
    state: Lifecycle.State,
    crossinline block: () -> R
): R
```

Run `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).withStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.Function0)` with this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` in a `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` of at least `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).withStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.Function0)` and resume with the result. Throws the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleDestroyedException` if the lifecycle has reached `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` by the time of the call or before `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).withStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.Function0)` is able to run.

## Extension properties

### Lifecycle.coroutineScope

Cmn Artifact: [androidx.lifecycle:lifecycle-common](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/Lifecycle.kt+symbol:coroutineScope) Added in [2.6.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.6.0)

```
val Lifecycle.coroutineScope: LifecycleCoroutineScope
```

`https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` tied to this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle`.

Canceled when the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` is destroyed. Bound to `Dispatchers.Main.immediate`.

### Lifecycle.eventFlow

Cmn Artifact: [androidx.lifecycle:lifecycle-common](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/Lifecycle.kt+symbol:eventFlow) Added in [2.7.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.7.0)

```
val Lifecycle.eventFlow: Flow<Lifecycle.Event>
```

Creates a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` of `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event`s dispatched by this `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle`.