# PausingDispatcherKt

Added in 2.6.0

*** ** * ** ***


```
public final class PausingDispatcherKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> [whenCreated](https://developer.android.com/reference/androidx/lifecycle/PausingDispatcherKt#(androidx.lifecycle.Lifecycle).whenCreated(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 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://developer.android.com/reference/androidx/annotation/NonNull T> block )` **This method 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. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> [whenCreated](https://developer.android.com/reference/androidx/lifecycle/PausingDispatcherKt#(androidx.lifecycle.LifecycleOwner).whenCreated(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 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://developer.android.com/reference/androidx/annotation/NonNull T> block )` **This method 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. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> [whenResumed](https://developer.android.com/reference/androidx/lifecycle/PausingDispatcherKt#(androidx.lifecycle.Lifecycle).whenResumed(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 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://developer.android.com/reference/androidx/annotation/NonNull T> block )` **This method 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. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> [whenResumed](https://developer.android.com/reference/androidx/lifecycle/PausingDispatcherKt#(androidx.lifecycle.LifecycleOwner).whenResumed(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 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://developer.android.com/reference/androidx/annotation/NonNull T> block )` **This method 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. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> [whenStarted](https://developer.android.com/reference/androidx/lifecycle/PausingDispatcherKt#(androidx.lifecycle.Lifecycle).whenStarted(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 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://developer.android.com/reference/androidx/annotation/NonNull T> block )` **This method 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. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> [whenStarted](https://developer.android.com/reference/androidx/lifecycle/PausingDispatcherKt#(androidx.lifecycle.LifecycleOwner).whenStarted(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 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://developer.android.com/reference/androidx/annotation/NonNull T> block )` **This method 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. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> [whenStateAtLeast](https://developer.android.com/reference/androidx/lifecycle/PausingDispatcherKt#(androidx.lifecycle.Lifecycle).whenStateAtLeast(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 minState, @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://developer.android.com/reference/androidx/annotation/NonNull T> block )` **This method 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. |

## Public methods

### whenCreated

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)

```
public static final @NonNull T <T extends Object> [whenCreated](https://developer.android.com/reference/androidx/lifecycle/PausingDispatcherKt#(androidx.lifecycle.Lifecycle).whenCreated(kotlin.coroutines.SuspendFunction1))(
    @NonNull Lifecycle receiver,
    @NonNull SuspendFunction1<@NonNull CoroutineScope, @NonNull T> block
)
```

> [!CAUTION]
> **This method 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/androidx/lifecycle/Lifecycle` is at least in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#CREATED` state.

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

### whenCreated

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)

```
public static final @NonNull T <T extends Object> [whenCreated](https://developer.android.com/reference/androidx/lifecycle/PausingDispatcherKt#(androidx.lifecycle.LifecycleOwner).whenCreated(kotlin.coroutines.SuspendFunction1))(
    @NonNull LifecycleOwner receiver,
    @NonNull SuspendFunction1<@NonNull CoroutineScope, @NonNull T> block
)
```

> [!CAUTION]
> **This method 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/androidx/lifecycle/LifecycleOwner`'s `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` is at least in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#CREATED` state.

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

### whenResumed

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)

```
public static final @NonNull T <T extends Object> [whenResumed](https://developer.android.com/reference/androidx/lifecycle/PausingDispatcherKt#(androidx.lifecycle.Lifecycle).whenResumed(kotlin.coroutines.SuspendFunction1))(
    @NonNull Lifecycle receiver,
    @NonNull SuspendFunction1<@NonNull CoroutineScope, @NonNull T> block
)
```

> [!CAUTION]
> **This method 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/androidx/lifecycle/Lifecycle` is at least in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#RESUMED` state.

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

### whenResumed

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)

```
public static final @NonNull T <T extends Object> [whenResumed](https://developer.android.com/reference/androidx/lifecycle/PausingDispatcherKt#(androidx.lifecycle.LifecycleOwner).whenResumed(kotlin.coroutines.SuspendFunction1))(
    @NonNull LifecycleOwner receiver,
    @NonNull SuspendFunction1<@NonNull CoroutineScope, @NonNull T> block
)
```

> [!CAUTION]
> **This method 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/androidx/lifecycle/LifecycleOwner`'s `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` is at least in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#RESUMED` state.

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

### whenStarted

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)

```
public static final @NonNull T <T extends Object> [whenStarted](https://developer.android.com/reference/androidx/lifecycle/PausingDispatcherKt#(androidx.lifecycle.Lifecycle).whenStarted(kotlin.coroutines.SuspendFunction1))(
    @NonNull Lifecycle receiver,
    @NonNull SuspendFunction1<@NonNull CoroutineScope, @NonNull T> block
)
```

> [!CAUTION]
> **This method 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/androidx/lifecycle/Lifecycle` is at least in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#STARTED` state.

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

### whenStarted

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)

```
public static final @NonNull T <T extends Object> [whenStarted](https://developer.android.com/reference/androidx/lifecycle/PausingDispatcherKt#(androidx.lifecycle.LifecycleOwner).whenStarted(kotlin.coroutines.SuspendFunction1))(
    @NonNull LifecycleOwner receiver,
    @NonNull SuspendFunction1<@NonNull CoroutineScope, @NonNull T> block
)
```

> [!CAUTION]
> **This method 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/androidx/lifecycle/LifecycleOwner`'s `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` is at least in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#STARTED` state.

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

### whenStateAtLeast

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)

```
public static final @NonNull T <T extends Object> [whenStateAtLeast](https://developer.android.com/reference/androidx/lifecycle/PausingDispatcherKt#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1))(
    @NonNull Lifecycle receiver,
    @NonNull Lifecycle.State minState,
    @NonNull SuspendFunction1<@NonNull CoroutineScope, @NonNull T> block
)
```

> [!CAUTION]
> **This method 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/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/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/androidx/lifecycle/Lifecycle`'s state is at least `https://developer.android.com/reference/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)`.

If the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` moves to a lesser state while the `https://developer.android.com/reference/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` is running, the `https://developer.android.com/reference/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/androidx/lifecycle/Lifecycle` reaches to a state greater or equal to `https://developer.android.com/reference/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/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/androidx/lifecycle/Lifecycle` is at least in `https://developer.android.com/reference/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)`.

If the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` is destroyed while the `https://developer.android.com/reference/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)` is suspended, the `https://developer.android.com/reference/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/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/androidx/lifecycle/Lifecycle` moves outside the desired state. It is recommended to check the `https://developer.android.com/reference/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/androidx/lifecycle/Lifecycle#currentState()` before accessing the UI. See the sample below for more details.

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

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | The return value of the \[block\] |