OnBackPressedDispatcher
public
final
class
OnBackPressedDispatcher
extends Object
java.lang.Object | |
↳ | androidx.activity.OnBackPressedDispatcher |
Dispatcher that can be used to register OnBackPressedCallback
instances for handling
the ComponentActivity.onBackPressed()
callback via composition.
public class FormEntryFragment extends Fragment { @Override public void onAttach(@NonNull Context context) { super.onAttach(context); OnBackPressedCallback callback = new OnBackPressedCallback( true // default to enabled ) { @Override public void handleOnBackPressed() { showAreYouSureDialog(); } }; requireActivity().getOnBackPressedDispatcher().addCallback( this, // LifecycleOwner callback); } }
Summary
Public constructors | |
---|---|
OnBackPressedDispatcher()
Create a new OnBackPressedDispatcher that dispatches System back button pressed events
to one or more |
|
OnBackPressedDispatcher(Runnable fallbackOnBackPressed)
Create a new OnBackPressedDispatcher that dispatches System back button pressed events
to one or more |
Public methods | |
---|---|
void
|
addCallback(LifecycleOwner owner, OnBackPressedCallback onBackPressedCallback)
Receive callbacks to a new |
void
|
addCallback(OnBackPressedCallback onBackPressedCallback)
Add a new |
boolean
|
hasEnabledCallbacks()
Checks if there is at least one |
void
|
onBackPressed()
Trigger a call to the currently added |
Inherited methods | |
---|---|
Public constructors
OnBackPressedDispatcher
public OnBackPressedDispatcher ()
Create a new OnBackPressedDispatcher that dispatches System back button pressed events
to one or more OnBackPressedCallback
instances.
OnBackPressedDispatcher
public OnBackPressedDispatcher (Runnable fallbackOnBackPressed)
Create a new OnBackPressedDispatcher that dispatches System back button pressed events
to one or more OnBackPressedCallback
instances.
Parameters | |
---|---|
fallbackOnBackPressed |
Runnable : The Runnable that should be triggered if
onBackPressed() is called when hasEnabledCallbacks() returns false.
|
Public methods
addCallback
public void addCallback (LifecycleOwner owner, OnBackPressedCallback onBackPressedCallback)
Receive callbacks to a new OnBackPressedCallback
when the given
LifecycleOwner
is at least started
.
This will automatically call addCallback(OnBackPressedCallback)
and
remove the callback as the lifecycle state changes.
As a corollary, if your lifecycle is already at least
started
, calling this method will result in an immediate
call to addCallback(OnBackPressedCallback)
.
When the LifecycleOwner
is destroyed
, it will
automatically be removed from the list of callbacks. The only time you would need to
manually call OnBackPressedCallback.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 |
LifecycleOwner : The LifecycleOwner which controls when the callback should be invoked |
onBackPressedCallback |
OnBackPressedCallback : The callback to add |
See also:
addCallback
public void addCallback (OnBackPressedCallback onBackPressedCallback)
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(LifecycleOwner, OnBackPressedCallback)
. It is expected that you
call OnBackPressedCallback.remove()
to manually remove your callback.
Parameters | |
---|---|
onBackPressedCallback |
OnBackPressedCallback : The callback to add |
See also:
hasEnabledCallbacks
public boolean hasEnabledCallbacks ()
Checks if there is at least one enabled
callback registered with this dispatcher.
Returns | |
---|---|
boolean |
True if there is at least one enabled callback. |
onBackPressed
public void onBackPressed ()
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
fallback Runnable set by the constructor
will be triggered.