Class OnBackPressedDispatcher
OnBackPressedCallback instances for handling
the host.onBackPressed() callback via composition.-
Constructor Summary
ConstructorsConstructorDescriptionCreate a new OnBackPressedDispatcher that dispatches System back button pressed events to one or moreOnBackPressedCallbackinstances.OnBackPressedDispatcher(Runnable fallbackOnBackPressed) Create a new OnBackPressedDispatcher that dispatches System back button pressed events to one or moreOnBackPressedCallbackinstances. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCallback(OnBackPressedCallback onBackPressedCallback) Add a newOnBackPressedCallback.voidaddCallback(LifecycleOwner owner, OnBackPressedCallback callback) booleanChecks if there is at least oneenabledcallback registered with this dispatcher.voidTrigger a call to the currently addedcallbacksin 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 moreOnBackPressedCallbackinstances. -
OnBackPressedDispatcher
Create a new OnBackPressedDispatcher that dispatches System back button pressed events to one or moreOnBackPressedCallbackinstances.- 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 addedOnBackPressedCallbackwill be the first callback to receive a callback ifonBackPressed()is called.This method is not
Lifecycleaware - 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 newOnBackPressedCallbackwhen the givenLifecycleOwneris 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
LifecycleOwnerisdestroyed, 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
destroyedwhen 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 oneenabledcallback registered with this dispatcher.- Returns:
- True if there is at least one enabled callback.
-
onBackPressed
Trigger a call to the currently addedcallbacksin reverse order in which they were added. Only if the most recently added callback is notenabledwill any previously added callback be called.If
hasEnabledCallbacks()isfalsewhen this method is called, the fallback Runnable set bythe constructorwill be triggered.
-