OnBackPressedDispatcherKt

Added in 1.0.0

public final class OnBackPressedDispatcherKt


Summary

Public methods

static final @NonNull OnBackPressedCallback
addCallback(
    @NonNull OnBackPressedDispatcher receiver,
    LifecycleOwner owner,
    boolean enabled,
    @NonNull Function1<@NonNull OnBackPressedCallbackUnit> onBackPressed
)

Creates and registers a new OnBackPressedCallback that invokes onBackPressed.

Public methods

addCallback

public static final @NonNull OnBackPressedCallback addCallback(
    @NonNull OnBackPressedDispatcher receiver,
    LifecycleOwner owner,
    boolean enabled,
    @NonNull Function1<@NonNull OnBackPressedCallbackUnit> onBackPressed
)

Creates and registers a new OnBackPressedCallback that invokes onBackPressed.

If a LifecycleOwner is provided, the callback’s enabled state automatically follows the lifecycle: it is enabled while the lifecycle is at least State.STARTED and disabled otherwise. The callback stays registered until the LifecycleOwner is destroyed.

A default enabled state can be supplied.

Legacy Behavior

To restore the legacy add/remove behavior, set ActivityFlags.isOnBackPressedLifecycleOrderMaintained to false. In legacy mode, the handler is added on Lifecycle.Event.ON_START and removed on Lifecycle.Event.ON_STOP, which may change dispatch ordering across lifecycle transitions.