dropUnlessResumed

Functions summary

() -> Unit
@Composable
dropUnlessResumed(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.RESUMED.

Cmn

Functions

dropUnlessResumed

@Composable
fun dropUnlessResumed(
    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.RESUMED. Otherwise, block is not invoked.

For Navigation users, it's recommended to safeguard navigate methods when using them while a composable is in transition as a result of navigation.

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

Button(
    onClick =
        dropUnlessResumed {
            // Run on clicks only when the lifecycle is at least RESUMED.
            // 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.RESUMED.

Returns
() -> Unit

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