Added in API level 33

OnBackInvokedDispatcher


public interface OnBackInvokedDispatcher

android.window.OnBackInvokedDispatcher


Dispatcher to register OnBackInvokedCallback instances for handling back invocations. It also provides interfaces to update the attributes of OnBackInvokedCallback. Attribute updates are proactively pushed to the window manager if they change the dispatch target (a.k.a. the callback to be invoked next), or its behavior.

Summary

Constants

int PRIORITY_DEFAULT

Default priority level of OnBackInvokedCallbacks.

int PRIORITY_OVERLAY

Priority level of OnBackInvokedCallbacks for overlays such as menus and navigation drawers that should receive back dispatch before non-overlays.

int PRIORITY_SYSTEM_NAVIGATION_OBSERVER

Priority level of OnBackInvokedCallbacks designed to observe system-level back handling.

Public methods

abstract void registerOnBackInvokedCallback(int priority, OnBackInvokedCallback callback)

Registers a OnBackInvokedCallback.

abstract void unregisterOnBackInvokedCallback(OnBackInvokedCallback callback)

Unregisters a OnBackInvokedCallback.

Constants

PRIORITY_DEFAULT

Added in API level 33
public static final int PRIORITY_DEFAULT

Default priority level of OnBackInvokedCallbacks.

Constant Value: 0 (0x00000000)

PRIORITY_OVERLAY

Added in API level 33
public static final int PRIORITY_OVERLAY

Priority level of OnBackInvokedCallbacks for overlays such as menus and navigation drawers that should receive back dispatch before non-overlays.

Constant Value: 1000000 (0x000f4240)

PRIORITY_SYSTEM_NAVIGATION_OBSERVER

Added in API level 36
public static final int PRIORITY_SYSTEM_NAVIGATION_OBSERVER

Priority level of OnBackInvokedCallbacks designed to observe system-level back handling.

Callbacks registered with this priority are invoked only when a back event is not consumed by any other registered OnBackInvokedCallback with a priority of PRIORITY_DEFAULT or higher. This means they are called when the system is about to perform its default back navigation action, such as finishing the current activity or executing a back-to-home animation.

These callbacks are purely observational. They do not consume the back event and cannot intercept it. Their invocation has no impact on the normal back navigation flow, making them suitable for logging, analytics, or triggering actions that should not prevent the system's own back handling.

The invocation order for multiple callbacks registered with this priority is undefined.

On API level 36 only, there is a restriction that only one callback with PRIORITY_SYSTEM_NAVIGATION_OBSERVER can be registered at a time. On API level 37 and higher, the number of registered observer callbacks is unrestricted.

Constant Value: -2 (0xfffffffe)

Public methods

registerOnBackInvokedCallback

Added in API level 33
public abstract void registerOnBackInvokedCallback (int priority, 
                OnBackInvokedCallback callback)

Registers a OnBackInvokedCallback. Within the same priority level, callbacks are invoked in the reverse order in which they are registered. Higher priority callbacks are invoked before lower priority ones.

Parameters
priority int: The priority of the callback. Value is PRIORITY_DEFAULT, PRIORITY_OVERLAY, or PRIORITY_SYSTEM_NAVIGATION_OBSERVER Value is 0 or greater

callback OnBackInvokedCallback: The callback to be registered. If the callback instance has been already registered, the existing instance (no matter its priority) will be unregistered and registered again. This value cannot be null.

Throws
IllegalArgumentException if the priority is negative.

unregisterOnBackInvokedCallback

Added in API level 33
public abstract void unregisterOnBackInvokedCallback (OnBackInvokedCallback callback)

Unregisters a OnBackInvokedCallback.

Parameters
callback OnBackInvokedCallback: The callback to be unregistered. Does nothing if the callback has not been registered. This value cannot be null.