OnBackPressedDispatcher
class OnBackPressedDispatcher
Dispatcher that can be used to register OnBackPressedCallback instances for handling the ComponentActivity.onBackPressed callback via composition.
class FormEntryFragment : Fragment() {
override fun onAttach(context: Context) {
super.onAttach(context)
val callback = object : OnBackPressedCallback(
true // default to enabled
) {
override fun handleOnBackPressed() {
showAreYouSureDialog()
}
}
requireActivity().onBackPressedDispatcher.addCallback(
this, // LifecycleOwner
callback
)
}
}
When constructing an instance of this class, the fallbackOnBackPressed can be set to receive a callback if onBackPressed is called when hasEnabledCallbacks returns false.
Summary
Public constructors |
|---|
OnBackPressedDispatcher(fallbackOnBackPressed: Runnable?) |
OnBackPressedDispatcher( |
Public functions |
|
|---|---|
Unit |
@MainThreadAdd a new |
Unit |
@MainThreadRegisters a new |
Unit |
|
Unit |
|
Unit |
|
Boolean |
Returns |
Unit |
Trigger a call to the currently added |
Unit |
@RequiresApi(value = 33)Sets the |
Extension functions |
|
|---|---|
OnBackPressedCallback |
OnBackPressedDispatcher.addCallback(Creates and registers a new |
Public constructors
OnBackPressedDispatcher
OnBackPressedDispatcher(fallbackOnBackPressed: Runnable? = null)
OnBackPressedDispatcher
OnBackPressedDispatcher(
fallbackOnBackPressed: Runnable?,
onHasEnabledCallbacksChanged: Consumer<Boolean>?
)
Public functions
addCallback
@MainThread
fun addCallback(onBackPressedCallback: OnBackPressedCallback): Unit
Add a new OnBackPressedCallback. Callbacks are invoked in the reverse order in which they are added, so this newly added OnBackPressedCallback will be the first callback to receive a callback if onBackPressed is called.
This method is not Lifecycle aware - if you'd like to ensure that you only get callbacks when at least started, use addCallback. It is expected that you call OnBackPressedCallback.remove to manually remove your callback.
| Parameters | |
|---|---|
onBackPressedCallback: OnBackPressedCallback |
The callback to add |
| See also | |
|---|---|
onBackPressed |
addCallback
@MainThread
fun addCallback(
owner: LifecycleOwner,
onBackPressedCallback: OnBackPressedCallback
): Unit
Registers a new OnBackPressedCallback that follows the lifecycle of the given LifecycleOwner.
The callback is registered once and stays attached for the lifetime of the LifecycleOwner. Its OnBackPressedCallback.isEnabled state automatically follows the lifecycle: it becomes enabled when the lifecycle is at least Lifecycle.State.STARTED and disabled otherwise.
When the LifecycleOwner is Lifecycle.State.DESTROYED, the callback is removed and lifecycle tracking stops. If the lifecycle is already destroyed when this method is called, the callback is not added.
Legacy Behavior
To restore the legacy add/remove behavior, set ActivityFlags.isOnBackPressedLifecycleOrderMaintained to false. In legacy mode, the handler is added on Lifecycle.Event.ON_START and removed on Lifecycle.Event.ON_STOP, which may change dispatch ordering across lifecycle transitions.
| Parameters | |
|---|---|
owner: LifecycleOwner |
The |
onBackPressedCallback: OnBackPressedCallback |
The callback to register. |
| See also | |
|---|---|
onBackPressed |
dispatchOnBackCancelled
@VisibleForTesting
@MainThread
fun dispatchOnBackCancelled(): Unit
dispatchOnBackProgressed
@VisibleForTesting
@MainThread
fun dispatchOnBackProgressed(backEvent: BackEventCompat): Unit
dispatchOnBackStarted
@VisibleForTesting
@MainThread
fun dispatchOnBackStarted(backEvent: BackEventCompat): Unit
hasEnabledCallbacks
@MainThread
fun hasEnabledCallbacks(): Boolean
Returns true if there is at least one enabled callback registered with this dispatcher.
| Returns | |
|---|---|
Boolean |
True if there is at least one enabled callback. |
onBackPressed
@MainThread
fun onBackPressed(): Unit
Trigger a call to the currently added callbacks in reverse order in which they were added. Only if the most recently added callback is not enabled will any previously added callback be called.
If hasEnabledCallbacks is false when this method is called, the fallbackOnBackPressed set by the constructor will be triggered.
setOnBackInvokedDispatcher
@RequiresApi(value = 33)
fun setOnBackInvokedDispatcher(invoker: OnBackInvokedDispatcher): Unit
Sets the OnBackInvokedDispatcher for handling system back for Android SDK T+.
| Parameters | |
|---|---|
invoker: OnBackInvokedDispatcher |
the OnBackInvokedDispatcher to be set on this dispatcher |
Extension functions
addCallback
fun OnBackPressedDispatcher.addCallback(
owner: LifecycleOwner? = null,
enabled: Boolean = true,
onBackPressed: OnBackPressedCallback.() -> Unit
): OnBackPressedCallback
Creates and registers a new OnBackPressedCallback that invokes onBackPressed.
If a LifecycleOwner is provided, the callback’s enabled state automatically follows the lifecycle: it is enabled while the lifecycle is at least State.STARTED and disabled otherwise. The callback stays registered until the LifecycleOwner is destroyed.
A default enabled state can be supplied.
Legacy Behavior
To restore the legacy add/remove behavior, set ActivityFlags.isOnBackPressedLifecycleOrderMaintained to false. In legacy mode, the handler is added on Lifecycle.Event.ON_START and removed on Lifecycle.Event.ON_STOP, which may change dispatch ordering across lifecycle transitions.