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(
    fallbackOnBackPressed: Runnable?,
    onHasEnabledCallbacksChanged: Consumer<Boolean>?
)

Public functions

Unit

Add a new OnBackPressedCallback.

Unit
@MainThread
addCallback(
    owner: LifecycleOwner,
    onBackPressedCallback: OnBackPressedCallback
)

Registers a new OnBackPressedCallback that follows the lifecycle of the given LifecycleOwner.

Unit
Unit
Unit
Boolean

Returns true if there is at least one enabled callback registered with this dispatcher.

Unit

Trigger a call to the currently added callbacks in reverse order in which they were added.

Unit

Sets the OnBackInvokedDispatcher for handling system back for Android SDK T+.

Extension functions

OnBackPressedCallback
OnBackPressedDispatcher.addCallback(
    owner: LifecycleOwner?,
    enabled: Boolean,
    onBackPressed: OnBackPressedCallback.() -> Unit
)

Creates and registers a new OnBackPressedCallback that invokes onBackPressed.

Public constructors

OnBackPressedDispatcher

Added in 1.0.0
OnBackPressedDispatcher(fallbackOnBackPressed: Runnable? = null)

OnBackPressedDispatcher

Added in 1.8.0
OnBackPressedDispatcher(
    fallbackOnBackPressed: Runnable?,
    onHasEnabledCallbacksChanged: Consumer<Boolean>?
)

Public functions

addCallback

Added in 1.0.0
@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

Added in 1.0.0
@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 LifecycleOwner that controls when the callback should be active.

onBackPressedCallback: OnBackPressedCallback

The callback to register.

See also
onBackPressed

dispatchOnBackCancelled

Added in 1.8.0
@VisibleForTesting
@MainThread
fun dispatchOnBackCancelled(): Unit

dispatchOnBackProgressed

Added in 1.8.0
@VisibleForTesting
@MainThread
fun dispatchOnBackProgressed(backEvent: BackEventCompat): Unit

dispatchOnBackStarted

Added in 1.8.0
@VisibleForTesting
@MainThread
fun dispatchOnBackStarted(backEvent: BackEventCompat): Unit

hasEnabledCallbacks

Added in 1.0.0
@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

Added in 1.0.0
@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

Added in 1.6.0
@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.