# rememberLifecycleOwner

Common/All

## Functions summary

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/rememberLifecycleOwner.composable#rememberLifecycleOwner(androidx.lifecycle.Lifecycle.State,androidx.lifecycle.LifecycleOwner)( maxLifecycle: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State, parent: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner? )` Remembers a new `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` with a lifecycle that is a child of the parent composition's lifecycle. | Cmn |

## Functions

### rememberLifecycleOwner

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/RememberLifecycleOwner.kt+function:rememberLifecycleOwner) Added in [2.10.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.10.0)

```
@Composable
fun rememberLifecycleOwner(
    maxLifecycle: Lifecycle.State = RESUMED,
    parent: LifecycleOwner? = LocalLifecycleOwner.current
): LifecycleOwner
```

Remembers a new `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` with a lifecycle that is a child of the parent composition's lifecycle.

This is useful for creating components (e.g., a map view) that need a lifecycle shorter than the host screen. The child lifecycle will never be in a state greater than its parent, and it can be further restricted by the `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#rememberLifecycleOwner(androidx.lifecycle.Lifecycle.State,androidx.lifecycle.LifecycleOwner)` parameter.

The `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` states are ordered: `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#INITIALIZED`\<`https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#CREATED`\<`https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#STARTED`\<`https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#RESUMED`. `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` is a terminal state. Setting a `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#rememberLifecycleOwner(androidx.lifecycle.Lifecycle.State,androidx.lifecycle.LifecycleOwner)` of `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#STARTED`, for example, ensures the child's lifecycle will never enter the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#RESUMED` state, even if the parent is `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#RESUMED`.

When the composable leaves the composition, the child lifecycle will be moved to `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED`. This ensures the child is properly cleaned up even if it is referenced outside the composition.

To provide the new `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` to a sub-composition, use `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocalProvider.composable#CompositionLocalProvider(kotlin.Array,kotlin.Function0)`:

```kotlin
import androidx.compose.material.Text
import androidx.compose.runtime.CompositionLocalProvider
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.rememberLifecycleOwner

// Create a LifecycleOwner tied to this composition and cap it at STARTED.
// Use this when a child should never go to RESUMED, even if the parent does.
val cappedLifecycleOwner = rememberLifecycleOwner(maxLifecycle = Lifecycle.State.STARTED)

// Make children see the capped Lifecycle instead of the parent's.
CompositionLocalProvider(LocalLifecycleOwner provides cappedLifecycleOwner) {
    // Will only report CREATED or STARTED, never RESUMED.
    LifecycleAwareText()
}
```

```kotlin
// Limit a component's lifecycle to STARTED
val startedLifecycleOwner = rememberLifecycleOwner(maxLifecycle = Lifecycle.State.STARTED)
CompositionLocalProvider(LocalLifecycleOwner provides startedLifecycleOwner) {
  // This component and its children will never be RESUMED,
  // even if the parent is.
  MyComposableThatObservesLifecycle()
}
```

**Null parent:** If `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#rememberLifecycleOwner(androidx.lifecycle.Lifecycle.State,androidx.lifecycle.LifecycleOwner)` is **EXPLICITLY** `null`, this creates a root lifecycle that runs independently and manages its own state.

```kotlin
import androidx.compose.material.Text
import androidx.compose.runtime.CompositionLocalProvider
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.rememberLifecycleOwner

// Create a LifecycleOwner with no parent.
// Use this when a child needs its own independent Lifecycle, managed only
// by whether it is in the composition. Parent state changes do not affect it.
val detachedLifecycleOwner =
    rememberLifecycleOwner(parent = null, maxLifecycle = Lifecycle.State.STARTED)

CompositionLocalProvider(LocalLifecycleOwner provides detachedLifecycleOwner) {
    // Stays STARTED until this composable leaves the composition,
    // even if the parent moves to a lower state.
    LifecycleAwareText()
}
```

| Parameters |
|---|---|
| `maxLifecycle: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State = RESUMED` | The maximum `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` this child lifecycle is allowed to enter. Defaults to `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#RESUMED`. |
| `parent: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner? = LocalLifecycleOwner.current` | The `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` to use as the parent, or null if it is a root. Defaults to the `https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary#LocalLifecycleOwner()`. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` | A new `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` that is remembered across compositions. |