dropUnlessStarted

Functions summary

() -> Unit
@Composable
dropUnlessStarted(lifecycleOwner: LifecycleOwner, block: () -> Unit)

Returns a new decorated function that will invoke the given block if the lifecycleOwner's lifecycle state is at least State.STARTED.

Cmn

Functions

dropUnlessStarted

@Composable
fun dropUnlessStarted(
    lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current,
    block: () -> Unit
): () -> Unit

Returns a new decorated function that will invoke the given block if the lifecycleOwner's lifecycle state is at least State.STARTED. Otherwise, block is not invoked.

import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.lifecycle.compose.dropUnlessStarted

Button(
    onClick =
        dropUnlessStarted {
            // Run on clicks only when the lifecycle is at least STARTED.
            // Example: navController.navigate("next_screen")
        }
) {
    Text(text = "Navigate to next screen")
}
Parameters
lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current

The LifecycleOwner whose lifecycle will be observed. Defaults to the current LocalLifecycleOwner.

block: () -> Unit

The callback to be executed when the observed lifecycle state is at least State.STARTED.

Returns
() -> Unit

A decorated function that invoke block only if the lifecycle state is at least State.STARTED.