OnBackPressedCallback
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(enabled: Boolean) |
Public functions |
|
|---|---|
open Unit |
Callback for handling the system UI generated equivalent to |
abstract Unit |
Callback for handling the |
open Unit |
@MainThreadCallback for handling the system UI generated equivalent to |
open Unit |
@MainThreadCallback for handling the system UI generated equivalent to |
Unit |
Removes this callback from any |
Public constructors
OnBackPressedCallback
OnBackPressedCallback(enabled: Boolean)
| Parameters | |
|---|---|
enabled: Boolean |
The default enabled state for this callback. |
Public functions
handleOnBackCancelled
@MainThread
open fun handleOnBackCancelled(): Unit
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
abstract fun handleOnBackPressed(): Unit
Callback for handling the OnBackPressedDispatcher.onBackPressed event.
handleOnBackProgressed
@MainThread
open fun handleOnBackProgressed(backEvent: BackEventCompat): Unit
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
open fun handleOnBackStarted(backEvent: BackEventCompat): Unit
Callback for handling the system UI generated equivalent to OnBackPressedDispatcher.dispatchOnBackStarted.
This will only be called by the framework on API 34 and above.
remove
@MainThread
fun remove(): Unit
Removes this callback from any OnBackPressedDispatcher it is currently added to.
Public properties
isEnabled
var isEnabled: Boolean
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.