Class OnBackPressedDispatcher
OnBackPressedCallback
instances for handling
the host.onBackPressed()
callback via composition.-
Constructor Summary
ConstructorDescriptionCreate a new OnBackPressedDispatcher that dispatches System back button pressed events to one or moreOnBackPressedCallback
instances.OnBackPressedDispatcher
(Runnable fallbackOnBackPressed) Create a new OnBackPressedDispatcher that dispatches System back button pressed events to one or moreOnBackPressedCallback
instances. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCallback
(OnBackPressedCallback onBackPressedCallback) Add a newOnBackPressedCallback
.void
addCallback
(LifecycleOwner owner, OnBackPressedCallback callback) boolean
Checks if there is at least oneenabled
callback registered with this dispatcher.void
Trigger a call to the currently addedcallbacks
in reverse order in which they were added.
-
Constructor Details
-
OnBackPressedDispatcher
public OnBackPressedDispatcher()Create a new OnBackPressedDispatcher that dispatches System back button pressed events to one or moreOnBackPressedCallback
instances. -
OnBackPressedDispatcher
Create a new OnBackPressedDispatcher that dispatches System back button pressed events to one or moreOnBackPressedCallback
instances.- Parameters:
fallbackOnBackPressed
- The Runnable that should be triggered ifonBackPressed()
is called whenhasEnabledCallbacks()
returns false.
-
-
Method Details
-
addCallback
Add a newOnBackPressedCallback
. Callbacks are invoked in the reverse order in which they are added, so this newly addedOnBackPressedCallback
will be the first callback to receive a callback ifonBackPressed()
is called.This method is not
Lifecycle
aware - if you'd like to ensure that you only get callbacks when at leaststarted
, useaddCallback(LifecycleOwner, OnBackPressedCallback)
. It is expected that you callOnBackPressedCallback.remove()
to manually remove your callback.- Parameters:
onBackPressedCallback
- The callback to add- See Also:
-
addCallback
@UiThread public void addCallback(@Nonnull LifecycleOwner owner, @Nonnull OnBackPressedCallback callback) Receive callbacks to a newOnBackPressedCallback
when the givenLifecycleOwner
is at leaststarted
.This will automatically call
addCallback(OnBackPressedCallback)
and remove the callback as the lifecycle state changes. As a corollary, if your lifecycle is already at leaststarted
, calling this method will result in an immediate call toaddCallback(OnBackPressedCallback)
.When the
LifecycleOwner
isdestroyed
, it will automatically be removed from the list of callbacks. The only time you would need to manually callOnBackPressedCallback.remove()
is if you'd like to remove the callback prior to destruction of the associated lifecycle.If the Lifecycle is already
destroyed
when this method is called, the callback will not be added.- Parameters:
owner
- The LifecycleOwner which controls when the callback should be invokedcallback
- The callback to add- See Also:
-
hasEnabledCallbacks
Checks if there is at least oneenabled
callback registered with this dispatcher.- Returns:
- True if there is at least one enabled callback.
-
onBackPressed
Trigger a call to the currently addedcallbacks
in reverse order in which they were added. Only if the most recently added callback is notenabled
will any previously added callback be called.If
hasEnabledCallbacks()
isfalse
when this method is called, the fallback Runnable set bythe constructor
will be triggered.
-