rememberLifecycleOwner

Functions summary

LifecycleOwner
@Composable
rememberLifecycleOwner(
    maxLifecycle: Lifecycle.State,
    parent: LifecycleOwner?
)

Remembers a new LifecycleOwner with a lifecycle that is a child of the parent composition's lifecycle.

Cmn

Functions

rememberLifecycleOwner

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

Remembers a new 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 maxLifecycle parameter.

The Lifecycle states are ordered: Lifecycle.State.INITIALIZED<Lifecycle.State.CREATED<Lifecycle.State.STARTED<Lifecycle.State.RESUMED. Lifecycle.State.DESTROYED is a terminal state. Setting a maxLifecycle of Lifecycle.State.STARTED, for example, ensures the child's lifecycle will never enter the Lifecycle.State.RESUMED state, even if the parent is RESUMED.

When the composable leaves the composition, the child lifecycle will be moved to DESTROYED. This ensures the child is properly cleaned up even if it is referenced outside the composition.

To provide the new LifecycleOwner to a sub-composition, use CompositionLocalProvider:

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()
}
// 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 parent is EXPLICITLY null, this creates a root lifecycle that runs independently and manages its own state.

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: Lifecycle.State = RESUMED

The maximum Lifecycle.State this child lifecycle is allowed to enter. Defaults to RESUMED.

parent: LifecycleOwner? = LocalLifecycleOwner.current

The LifecycleOwner to use as the parent, or null if it is a root. Defaults to the LocalLifecycleOwner.

Returns
LifecycleOwner

A new LifecycleOwner that is remembered across compositions.