OnBackPressedCallback
public abstract class OnBackPressedCallback
Class for handling 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 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 | |
|---|---|
getOnBackPressedDispatcher |
ComponentActivity#getOnBackPressedDispatcher() |
Summary
Public constructors |
|
|---|---|
OnBackPressedCallback(@NonNull boolean enabled)Create a |
|
Public methods |
|
|---|---|
abstract @MainThread @NonNull void |
Callback for handling the |
final @MainThread @NonNull boolean |
Checks whether this callback should be considered enabled. |
final @MainThread @NonNull void |
remove()Removes this callback from any |
final @MainThread @NonNull void |
setEnabled(@NonNull boolean enabled)Set the enabled state of the callback. |
Public constructors
OnBackPressedCallback
public OnBackPressedCallback(@NonNull boolean enabled)
Create a OnBackPressedCallback.
| Parameters | |
|---|---|
@NonNull boolean enabled |
The default enabled state for this callback. |
| See also | |
|---|---|
setEnabled |
#setEnabled(boolean) |
Public methods
handleOnBackPressed
@MainThread
@NonNull
public abstract @MainThread void handleOnBackPressed()
Callback for handling the onBackPressed event.
isEnabled
@MainThread
@NonNull
public final @MainThread boolean isEnabled()
Checks whether this callback should be considered enabled. Only when this callback is enabled will it receive callbacks to handleOnBackPressed.
| Returns | |
|---|---|
@MainThread boolean |
Whether this callback should be considered enabled. |
remove
@MainThread
@NonNull
public final @MainThread void remove()
Removes this callback from any OnBackPressedDispatcher it is currently added to.
setEnabled
@MainThread
@NonNull
public final @MainThread void setEnabled(@NonNull boolean enabled)
Set 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 addCallback which controls when the callback is added and removed to the dispatcher.
| Parameters | |
|---|---|
@NonNull boolean enabled |
whether the callback should be considered enabled |