# RepeatOnLifecycleKt

Added in 2.8.0

*** ** * ** ***


```
public final class RepeatOnLifecycleKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final void` | `https://developer.android.com/reference/androidx/lifecycle/RepeatOnLifecycleKt#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State state, @https://developer.android.com/reference/androidx/annotation/NonNull SuspendFunction1<@https://developer.android.com/reference/androidx/annotation/NonNull 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> block )` Runs the given `https://developer.android.com/reference/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/androidx/lifecycle/Lifecycle` is at least at `https://developer.android.com/reference/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/androidx/lifecycle/Lifecycle` is `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#DESTROYED`. |
| `static final void` | `https://developer.android.com/reference/androidx/lifecycle/RepeatOnLifecycleKt#(androidx.lifecycle.LifecycleOwner).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State state, @https://developer.android.com/reference/androidx/annotation/NonNull SuspendFunction1<@https://developer.android.com/reference/androidx/annotation/NonNull 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> block )` `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner`'s extension function for `https://developer.android.com/reference/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` to allow an easier call to the API from LifecycleOwners such as Activities and Fragments. |

## Public methods

### repeatOnLifecycle

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) Added in [2.8.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.8.0)

```
public static final void repeatOnLifecycle(
    @NonNull Lifecycle receiver,
    @NonNull Lifecycle.State state,
    @NonNull SuspendFunction1<@NonNull CoroutineScope, Unit> block
)
```

Runs the given `https://developer.android.com/reference/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/androidx/lifecycle/Lifecycle` is at least at `https://developer.android.com/reference/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/androidx/lifecycle/Lifecycle` is `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#DESTROYED`.

The `https://developer.android.com/reference/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.

```
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/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/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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State state` | `https://developer.android.com/reference/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. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull SuspendFunction1<@https://developer.android.com/reference/androidx/annotation/NonNull 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> block` | The block to run when the lifecycle is at least in `https://developer.android.com/reference/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` state. |

### repeatOnLifecycle

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) Added in [2.8.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.8.0)

```
public static final void repeatOnLifecycle(
    @NonNull LifecycleOwner receiver,
    @NonNull Lifecycle.State state,
    @NonNull SuspendFunction1<@NonNull CoroutineScope, Unit> block
)
```

`https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner`'s extension function for `https://developer.android.com/reference/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` to allow an easier call to the API from LifecycleOwners such as Activities and Fragments.

```
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 {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                uiStateFlow.collect { uiState ->
                    updateUi(uiState)
                }
            }
        }
    }
}
```

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