Register now for Android Dev Summit 2019!

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

Create a new OnBackPressedDispatcher that dispatches System back button pressed events to one or more OnBackPressedCallback instances.

<init>(@Nullable fallbackOnBackPressed: Runnable?)

Create a new OnBackPressedDispatcher that dispatches System back button pressed events to one or more OnBackPressedCallback instances.

Public methods

Unit
addCallback(@NonNull onBackPressedCallback: OnBackPressedCallback)

Add a new OnBackPressedCallback.

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

Receive callbacks to a new OnBackPressedCallback when the given LifecycleOwner is at least started.

Boolean

Checks 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.

Extension functions

From androidx.activity
OnBackPressedCallback
OnBackPressedDispatcher.addCallback(owner: LifecycleOwner? = null, enabled: Boolean = true, onBackPressed: OnBackPressedCallback.() -> Unit)

Create and add a new OnBackPressedCallback that calls onBackPressed in OnBackPressedCallback.handleOnBackPressed.

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 LifecycleOwner: 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.

It is strongly recommended to call hasEnabledCallbacks() prior to calling this method to determine if there are any enabled callbacks that will be triggered by this method as calling this method.