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.
| See also | |
|---|---|
OnBackPressedDispatcher |
Summary
Public constructors |
|---|
OnBackPressedCallback(boolean enabled) |
Public methods |
|
|---|---|
void |
Callback for handling the system UI generated equivalent to |
abstract void |
Callback for handling the |
void |
@MainThreadCallback for handling the system UI generated equivalent to |
void |
@MainThreadCallback for handling the system UI generated equivalent to |
final boolean |
The enabled state of the callback. |
final void |
Removes this callback from any |
final void |
@MainThreadThe enabled state of the callback. |
Public constructors
OnBackPressedCallback
public OnBackPressedCallback(boolean enabled)
| Parameters | |
|---|---|
boolean enabled |
The default enabled state for this callback. |
Public methods
handleOnBackCancelled
@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
@MainThread
public abstract void handleOnBackPressed()
Callback for handling the OnBackPressedDispatcher.onBackPressed event.
handleOnBackProgressed
@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
@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
@MainThread
public final boolean isEnabled()
The enabled state of the callback. Only when this callback is enabled will it receive callbacks to handleOnBackPressed.
When registered with a androidx.lifecycle.LifecycleOwner, the callback is only active when both this property is true and the androidx.lifecycle.Lifecycle is at least androidx.lifecycle.Lifecycle.State.STARTED.
remove
@MainThread
public final void remove()
Removes this callback from any OnBackPressedDispatcher it is currently added to.
setEnabled
@MainThread
public final void setEnabled(boolean value)
The enabled state of the callback. Only when this callback is enabled will it receive callbacks to handleOnBackPressed.
When registered with a androidx.lifecycle.LifecycleOwner, the callback is only active when both this property is true and the androidx.lifecycle.Lifecycle is at least androidx.lifecycle.Lifecycle.State.STARTED.