OnBackPressedDispatcher


public final class OnBackPressedDispatcher


Dispatcher that can be used to register OnBackPressedCallback instances for handling the ComponentActivity.onBackPressed callback via composition.

class FormEntryFragment : Fragment() {
override fun onAttach(context: Context) {
super.onAttach(context)
val callback = object : OnBackPressedCallback(
true // default to enabled
) {
override fun handleOnBackPressed() {
showAreYouSureDialog()
}
}
requireActivity().onBackPressedDispatcher.addCallback(
this, // LifecycleOwner
callback
)
}
}

When constructing an instance of this class, the fallbackOnBackPressed can be set to receive a callback if onBackPressed is called when hasEnabledCallbacks returns false.

Summary

Public constructors

OnBackPressedDispatcher(Runnable fallbackOnBackPressed)
OnBackPressedDispatcher(
    Runnable fallbackOnBackPressed,
    Consumer<@NonNull Boolean> onHasEnabledCallbacksChanged
)

Public methods

final void

Add a new OnBackPressedCallback.

final void
@MainThread
addCallback(
    @NonNull LifecycleOwner owner,
    @NonNull OnBackPressedCallback onBackPressedCallback
)

Receive callbacks to a new OnBackPressedCallback when the given LifecycleOwner is at least started.

final void
final void
final void
final boolean

Returns true if there is at least one enabled callback registered with this dispatcher.

final void

Trigger a call to the currently added callbacks in reverse order in which they were added.

final void

Sets the OnBackInvokedDispatcher for handling system back for Android SDK T+.

Extension functions

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

Create and add a new OnBackPressedCallback that calls onBackPressed in OnBackPressedCallback.handleOnBackPressed.

Public constructors

OnBackPressedDispatcher

Added in 1.0.0
public OnBackPressedDispatcher(Runnable fallbackOnBackPressed)

OnBackPressedDispatcher

Added in 1.8.0
public OnBackPressedDispatcher(
    Runnable fallbackOnBackPressed,
    Consumer<@NonNull Boolean> onHasEnabledCallbacksChanged
)

Public methods

addCallback

Added in 1.0.0
@MainThread
public final void addCallback(@NonNull OnBackPressedCallback onBackPressedCallback)

Add a new OnBackPressedCallback. Callbacks are invoked in the reverse order in which they are added, so this newly added OnBackPressedCallback will be the first callback to receive a callback if onBackPressed is called.

This method is not Lifecycle aware - if you'd like to ensure that you only get callbacks when at least started, use addCallback. It is expected that you call OnBackPressedCallback.remove to manually remove your callback.

Parameters
@NonNull OnBackPressedCallback onBackPressedCallback

The callback to add

See also
onBackPressed

addCallback

Added in 1.0.0
@MainThread
public final void addCallback(
    @NonNull LifecycleOwner owner,
    @NonNull OnBackPressedCallback onBackPressedCallback
)

Receive callbacks to a new OnBackPressedCallback when the given LifecycleOwner is at least started.

This will automatically call addCallback and remove the callback as the lifecycle state changes. As a corollary, if your lifecycle is already at least started, calling this method will result in an immediate call to addCallback.

When the LifecycleOwner is destroyed, it will automatically be removed from the list of callbacks. The only time you would need to manually call OnBackPressedCallback.remove is if you'd like to remove the callback prior to destruction of the associated lifecycle.

If the Lifecycle is already destroyed when this method is called, the callback will not be added.

Parameters
@NonNull LifecycleOwner owner

The LifecycleOwner which controls when the callback should be invoked

@NonNull OnBackPressedCallback onBackPressedCallback

The callback to add

See also
onBackPressed

dispatchOnBackCancelled

Added in 1.8.0
@VisibleForTesting
@MainThread
public final void dispatchOnBackCancelled()

dispatchOnBackProgressed

Added in 1.8.0
@VisibleForTesting
@MainThread
public final void dispatchOnBackProgressed(@NonNull BackEventCompat backEvent)

dispatchOnBackStarted

Added in 1.8.0
@VisibleForTesting
@MainThread
public final void dispatchOnBackStarted(@NonNull BackEventCompat backEvent)

hasEnabledCallbacks

Added in 1.0.0
@MainThread
public final boolean hasEnabledCallbacks()

Returns true if there is at least one enabled callback registered with this dispatcher.

Returns
boolean

True if there is at least one enabled callback.

onBackPressed

Added in 1.0.0
@MainThread
public final void onBackPressed()

Trigger a call to the currently added callbacks in reverse order in which they were added. Only if the most recently added callback is not enabled will any previously added callback be called.

If hasEnabledCallbacks is false when this method is called, the fallbackOnBackPressed set by the constructor will be triggered.

setOnBackInvokedDispatcher

Added in 1.6.0
@RequiresApi(value = 33)
public final void setOnBackInvokedDispatcher(@NonNull OnBackInvokedDispatcher invoker)

Sets the OnBackInvokedDispatcher for handling system back for Android SDK T+.

Parameters
@NonNull OnBackInvokedDispatcher invoker

the OnBackInvokedDispatcher to be set on this dispatcher

Extension functions

OnBackPressedDispatcherKt.addCallback

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

Create and add a new OnBackPressedCallback that calls onBackPressed in OnBackPressedCallback.handleOnBackPressed.

If an owner is specified, the callback will only be added when the Lifecycle is androidx.lifecycle.Lifecycle.State.STARTED.

A default enabled state can be supplied.