ListenToComposableLifecycle

fun ListenToComposableLifecycle(lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current, onEvent: (LifecycleOwner, Lifecycle.Event) -> Unit)

Observes the lifecycle of a Composable function.

This function allows you to execute specific actions in response to lifecycle events of a Composable, such as when it's created, started, resumed, paused, stopped, or destroyed.

It uses a DisposableEffect to ensure that the lifecycle observer is properly added when the Composable enters the composition and removed when it leaves, preventing memory leaks.

Parameters

lifecycleOwner

The LifecycleOwner whose lifecycle events are to be observed. Defaults to the LocalLifecycleOwner of the current composition. This allows you to observe the lifecycle of the Composable itself or any other LifecycleOwner available in the scope.

onEvent

A lambda function that will be invoked when a lifecycle event occurs. It receives two parameters: - source: The LifecycleOwner that triggered the event. - event: The Lifecycle.Event that occurred (e.g., ON_CREATE, ON_START).

Example usage:

ListenToComposableLifecycle { source, event ->
when (event) {
Lifecycle.Event.ON_START -> {
// Composable has started
}
Lifecycle.Event.ON_STOP -> {
// Composable has stopped
}