# LifecycleCoroutineScope

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

*** ** * ** ***


```
public abstract class LifecycleCoroutineScope implements CoroutineScope
```

<br />

*** ** * ** ***

`https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` tied to a `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` and `Dispatchers.Main.immediate`.

Canceled when the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` is destroyed.

## Summary

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html` | `[launchWhenCreated](https://developer.android.com/reference/androidx/lifecycle/LifecycleCoroutineScope#launchWhenCreated(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://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block )` **This method is deprecated.** launchWhenCreated is deprecated as it can lead to wasted resources in some cases. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html` | `[launchWhenResumed](https://developer.android.com/reference/androidx/lifecycle/LifecycleCoroutineScope#launchWhenResumed(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://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block )` **This method is deprecated.** launchWhenResumed is deprecated as it can lead to wasted resources in some cases. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html` | `[launchWhenStarted](https://developer.android.com/reference/androidx/lifecycle/LifecycleCoroutineScope#launchWhenStarted(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://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block )` **This method is deprecated.** launchWhenStarted is deprecated as it can lead to wasted resources in some cases. |

### Inherited methods

From [kotlinx.coroutines.CoroutineScope](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html) `abstract @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/get-coroutine-context.html()` `abstract @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/get-coroutine-context.html()` `abstract @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/get-coroutine-context.html()`

## Public methods

### launchWhenCreated

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

```
public final @NonNull Job [launchWhenCreated](https://developer.android.com/reference/androidx/lifecycle/LifecycleCoroutineScope#launchWhenCreated(kotlin.coroutines.SuspendFunction1))(
    @NonNull SuspendFunction1<@NonNull CoroutineScope, Unit> block
)
```

> [!CAUTION]
> **This method is deprecated.**   
> launchWhenCreated is deprecated as it can lead to wasted resources in some cases. Replace with suspending repeatOnLifecycle to run the block whenever the Lifecycle state is at least Lifecycle.State.CREATED.

Launches and runs the given block when the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` controlling this `https://developer.android.com/reference/androidx/lifecycle/LifecycleCoroutineScope` is at least in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#CREATED` state.

The returned `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html` will be cancelled when the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` is destroyed.

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

### launchWhenResumed

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

```
public final @NonNull Job [launchWhenResumed](https://developer.android.com/reference/androidx/lifecycle/LifecycleCoroutineScope#launchWhenResumed(kotlin.coroutines.SuspendFunction1))(
    @NonNull SuspendFunction1<@NonNull CoroutineScope, Unit> block
)
```

> [!CAUTION]
> **This method is deprecated.**   
> launchWhenResumed is deprecated as it can lead to wasted resources in some cases. Replace with suspending repeatOnLifecycle to run the block whenever the Lifecycle state is at least Lifecycle.State.RESUMED.

Launches and runs the given block when the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` controlling this `https://developer.android.com/reference/androidx/lifecycle/LifecycleCoroutineScope` is at least in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#RESUMED` state.

The returned `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html` will be cancelled when the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` is destroyed.

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

### launchWhenStarted

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

```
public final @NonNull Job [launchWhenStarted](https://developer.android.com/reference/androidx/lifecycle/LifecycleCoroutineScope#launchWhenStarted(kotlin.coroutines.SuspendFunction1))(
    @NonNull SuspendFunction1<@NonNull CoroutineScope, Unit> block
)
```

> [!CAUTION]
> **This method is deprecated.**   
> launchWhenStarted is deprecated as it can lead to wasted resources in some cases. Replace with suspending repeatOnLifecycle to run the block whenever the Lifecycle state is at least Lifecycle.State.STARTED.

Launches and runs the given block when the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` controlling this `https://developer.android.com/reference/androidx/lifecycle/LifecycleCoroutineScope` is at least in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#STARTED` state.

The returned `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html` will be cancelled when the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` is destroyed.

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