OnBackPressedCallback


public abstract class OnBackPressedCallback


Class for handling OnBackPressedDispatcher.onBackPressed callbacks without strongly coupling that implementation to a subclass of ComponentActivity.

This class maintains its own enabled state. Only when this callback is enabled will it receive callbacks to handleOnBackPressed.

Note that the enabled state is an additional layer on top of the androidx.lifecycle.LifecycleOwner passed to OnBackPressedDispatcher.addCallback which controls when the callback is added and removed to the dispatcher.

By calling remove, this callback will be removed from any OnBackPressedDispatcher it has been added to. It is strongly recommended to instead disable this callback to handle temporary changes in state.

Summary

Public constructors

OnBackPressedCallback(boolean enabled)

Public methods

void

Callback for handling the system UI generated equivalent to OnBackPressedDispatcher.dispatchOnBackCancelled.

abstract void

Callback for handling the OnBackPressedDispatcher.onBackPressed event.

void

Callback for handling the system UI generated equivalent to OnBackPressedDispatcher.dispatchOnBackProgressed.

void

Callback for handling the system UI generated equivalent to OnBackPressedDispatcher.dispatchOnBackStarted.

final boolean

The enabled state of the callback.

final void

Removes this callback from any OnBackPressedDispatcher it is currently added to.

final void
@MainThread
setEnabled(boolean isEnabled)

The enabled state of the callback.

Public constructors

OnBackPressedCallback

Added in 1.0.0
public OnBackPressedCallback(boolean enabled)
Parameters
boolean enabled

The default enabled state for this callback.

Public methods

handleOnBackCancelled

Added in 1.8.0
@MainThread
public void handleOnBackCancelled()

Callback for handling the system UI generated equivalent to OnBackPressedDispatcher.dispatchOnBackCancelled.

This will only be called by the framework on API 34 and above.

handleOnBackPressed

Added in 1.0.0
@MainThread
public abstract void handleOnBackPressed()

Callback for handling the OnBackPressedDispatcher.onBackPressed event.

handleOnBackProgressed

Added in 1.8.0
@MainThread
public void handleOnBackProgressed(@NonNull BackEventCompat backEvent)

Callback for handling the system UI generated equivalent to OnBackPressedDispatcher.dispatchOnBackProgressed.

This will only be called by the framework on API 34 and above.

handleOnBackStarted

Added in 1.8.0
@MainThread
public void handleOnBackStarted(@NonNull BackEventCompat backEvent)

Callback for handling the system UI generated equivalent to OnBackPressedDispatcher.dispatchOnBackStarted.

This will only be called by the framework on API 34 and above.

isEnabled

Added in 1.0.0
@MainThread
public final boolean isEnabled()

The enabled state of the callback. Only when this callback is enabled will it receive callbacks to handleOnBackPressed.

Note that the enabled state is an additional layer on top of the androidx.lifecycle.LifecycleOwner passed to OnBackPressedDispatcher.addCallback which controls when the callback is added and removed to the dispatcher.

remove

Added in 1.0.0
@MainThread
public final void remove()

Removes this callback from any OnBackPressedDispatcher it is currently added to.

setEnabled

Added in 1.0.0
@MainThread
public final void setEnabled(boolean isEnabled)

The enabled state of the callback. Only when this callback is enabled will it receive callbacks to handleOnBackPressed.

Note that the enabled state is an additional layer on top of the androidx.lifecycle.LifecycleOwner passed to OnBackPressedDispatcher.addCallback which controls when the callback is added and removed to the dispatcher.