OnBackPressedDispatcher
class OnBackPressedDispatcher
kotlin.Any | |
↳ | 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 | |
---|---|
<init>() Create a new OnBackPressedDispatcher that dispatches System back button pressed events to one or more |
|
Create a new OnBackPressedDispatcher that dispatches System back button pressed events to one or more |
Public methods | |
---|---|
Unit |
addCallback(@NonNull onBackPressedCallback: OnBackPressedCallback) Add a new |
Unit |
addCallback(@NonNull owner: LifecycleOwner, @NonNull onBackPressedCallback: OnBackPressedCallback) Receive callbacks to a new |
Boolean |
Checks if there is at least one |
Unit |
Trigger a call to the currently added |
Extension functions | ||
---|---|---|
From androidx.activity
|
Public constructors
<init>
OnBackPressedDispatcher()
Create a new OnBackPressedDispatcher that dispatches System back button pressed events to one or more OnBackPressedCallback
instances.
<init>
OnBackPressedDispatcher(@Nullable fallbackOnBackPressed: Runnable?)
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
@MainThread fun addCallback(@NonNull 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(LifecycleOwner, OnBackPressedCallback)
. It is expected that you call OnBackPressedCallback#remove()
to manually remove your callback.
Parameters | |
---|---|
onBackPressedCallback |
OnBackPressedCallback: The callback to add |
See Also
addCallback
@MainThread fun addCallback(
@NonNull owner: LifecycleOwner,
@NonNull onBackPressedCallback: OnBackPressedCallback
): Unit
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
hasEnabledCallbacks
@MainThread fun hasEnabledCallbacks(): Boolean
Checks if there is at least one enabled
callback registered with this dispatcher.
Return | |
---|---|
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 fallback Runnable set by the constructor
will be triggered.