# OnBackPressedDispatcher

Artifact: [androidx.activity:activity](https://developer.android.com/jetpack/androidx/releases/activity) [View Source](https://cs.android.com/search?q=file:androidx/activity/OnBackPressedDispatcher.kt+class:androidx.activity.OnBackPressedDispatcher) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/activity#1.0.0)

*** ** * ** ***


```
public final class OnBackPressedDispatcher
```

<br />

*** ** * ** ***

Dispatcher that can be used to register `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback` instances for handling the `https://developer.android.com/reference/androidx/activity/ComponentActivity#onBackPressed()` callback via composition.

```
class FormEntryFragment : Fragment() {
  override fun onAttach(context: Context) {
    super.onAttach(context)
    val callback = object : OnBackPressedCallback(
      true // default to enabled
    ) {
      override fun handleOnBackPressed() {
        showAreYouSureDialog()
      }
    }
    requireActivity().onBackPressedDispatcher.addCallback(
      this, // LifecycleOwner
      callback
    )
  }
}
```

When constructing an instance of this class, the `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#fallbackOnBackPressed()` can be set to receive a callback if `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#onBackPressed()` is called when `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#hasEnabledCallbacks()` returns `false`.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#OnBackPressedDispatcher(java.lang.Runnable)(https://developer.android.com/reference/java/lang/Runnable.html fallbackOnBackPressed)` |
| `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#OnBackPressedDispatcher(java.lang.Runnable,androidx.core.util.Consumer)( https://developer.android.com/reference/java/lang/Runnable.html fallbackOnBackPressed, https://developer.android.com/reference/androidx/core/util/Consumer<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html> onHasEnabledCallbacksChanged )` |

| ### Public methods |
|---|---|
| `final void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#addCallback(androidx.activity.OnBackPressedCallback)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/OnBackPressedCallback onBackPressedCallback)` Add a new `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback`. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#addCallback(androidx.lifecycle.LifecycleOwner,androidx.activity.OnBackPressedCallback)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner owner, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/OnBackPressedCallback onBackPressedCallback )` Registers a new `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback` that follows the lifecycle of the given `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/navigationevent/NavigationEventDispatcher` | `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#asNavigationEventDispatcher()()` Bridges this `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher` to `https://developer.android.com/reference/androidx/navigationevent/NavigationEventDispatcher`. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/VisibleForTesting @https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#dispatchOnBackCancelled()()` |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/VisibleForTesting @https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#dispatchOnBackProgressed(androidx.activity.BackEventCompat)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/BackEventCompat backEvent)` |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/VisibleForTesting @https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#dispatchOnBackStarted(androidx.activity.BackEventCompat)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/BackEventCompat backEvent)` |
| `final boolean` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#hasEnabledCallbacks()()` Returns `true` if there is at least one `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#isEnabled()` callback registered with this dispatcher. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#onBackPressed()()` Trigger a call to the currently added `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback` in reverse order in which they were added. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 33) https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#setOnBackInvokedDispatcher(android.window.OnBackInvokedDispatcher)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/window/OnBackInvokedDispatcher.html invoker)` Sets the `https://developer.android.com/reference/android/window/OnBackInvokedDispatcher.html` for handling system back for Android SDK T+. |

| ### Extension functions |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/OnBackPressedCallback` | `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcherKt.https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#(androidx.activity.OnBackPressedDispatcher).addCallback(androidx.lifecycle.LifecycleOwner,kotlin.Boolean,kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher receiver, https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner owner, boolean enabled, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/OnBackPressedCallback, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onBackPressed )` Creates and registers a new `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback` that invokes `https://developer.android.com/reference/androidx/activity/package-summary#(androidx.activity.OnBackPressedDispatcher).addCallback(androidx.lifecycle.LifecycleOwner,kotlin.Boolean,kotlin.Function1)`. |

## Public constructors

### OnBackPressedDispatcher

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/activity#1.0.0)

```
public OnBackPressedDispatcher(Runnable fallbackOnBackPressed)
```

### OnBackPressedDispatcher

Added in [1.8.0](https://developer.android.com/jetpack/androidx/releases/activity#1.8.0)

```
public OnBackPressedDispatcher(
    Runnable fallbackOnBackPressed,
    Consumer<@NonNull Boolean> onHasEnabledCallbacksChanged
)
```

## Public methods

### addCallback

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/activity#1.0.0)

```
@MainThread
public final void addCallback(@NonNull OnBackPressedCallback onBackPressedCallback)
```

Add a new `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback`. Callbacks are invoked in the reverse order in which they are added, so this newly added `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback` will be the first callback to receive a callback if `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#onBackPressed()` is called.

This method is **not** `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` aware - if you'd like to ensure that you only get callbacks when at least `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#STARTED`, use `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#addCallback(androidx.activity.OnBackPressedCallback)`. It is expected that you call `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#remove()` to manually remove your callback.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/OnBackPressedCallback onBackPressedCallback` | The callback to add |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#onBackPressed()` |   |

### addCallback

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/activity#1.0.0)

```
@MainThread
public final void addCallback(
    @NonNull LifecycleOwner owner,
    @NonNull OnBackPressedCallback onBackPressedCallback
)
```

Registers a new `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback` that follows the lifecycle of the given `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner`.

The callback is registered once and stays attached for the lifetime of the `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner`. Its `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#isEnabled()` state automatically follows the lifecycle: it becomes enabled when the lifecycle is at least `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#STARTED` and disabled otherwise.

When the `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` is `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#DESTROYED`, the callback is removed and lifecycle tracking stops. If the lifecycle is already destroyed when this method is called, the callback is not added.

## Legacy Behavior

To restore the legacy add/remove behavior, set `https://developer.android.com/reference/androidx/activity/ActivityFlags#isOnBackPressedLifecycleOrderMaintained()` to `false`. In legacy mode, the handler is added on `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_START` and removed on `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_STOP`, which may change dispatch ordering across lifecycle transitions.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner owner` | The `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` that controls when the callback should be active. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/OnBackPressedCallback onBackPressedCallback` | The callback to register. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#onBackPressed()` |   |

### asNavigationEventDispatcher

Added in [1.14.0-alpha03](https://developer.android.com/jetpack/androidx/releases/activity#1.14.0-alpha03)

```
public final @NonNull NavigationEventDispatcher asNavigationEventDispatcher()
```

Bridges this `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher` to `https://developer.android.com/reference/androidx/navigationevent/NavigationEventDispatcher`.

Returns this `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher` as a `https://developer.android.com/reference/androidx/navigationevent/NavigationEventDispatcher`. Handlers registered on this dispatcher and back events dispatched by it are coordinated through this instance.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/navigationevent/NavigationEventDispatcher` | this dispatcher as a `https://developer.android.com/reference/androidx/navigationevent/NavigationEventDispatcher` |

### dispatchOnBackCancelled

Added in [1.8.0](https://developer.android.com/jetpack/androidx/releases/activity#1.8.0)

```
@VisibleForTesting
@MainThread
public final void dispatchOnBackCancelled()
```

### dispatchOnBackProgressed

Added in [1.8.0](https://developer.android.com/jetpack/androidx/releases/activity#1.8.0)

```
@VisibleForTesting
@MainThread
public final void dispatchOnBackProgressed(@NonNull BackEventCompat backEvent)
```

### dispatchOnBackStarted

Added in [1.8.0](https://developer.android.com/jetpack/androidx/releases/activity#1.8.0)

```
@VisibleForTesting
@MainThread
public final void dispatchOnBackStarted(@NonNull BackEventCompat backEvent)
```

### hasEnabledCallbacks

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/activity#1.0.0)

```
@MainThread
public final boolean hasEnabledCallbacks()
```

Returns `true` if there is at least one `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#isEnabled()` callback registered with this dispatcher.

| Returns |
|---|---|
| `boolean` | True if there is at least one enabled callback. |

### onBackPressed

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/activity#1.0.0)

```
@MainThread
public final void onBackPressed()
```

Trigger a call to the currently added `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback` in reverse order in which they were added. Only if the most recently added callback is not `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#isEnabled()` will any previously added callback be called.

If `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#hasEnabledCallbacks()` is `false` when this method is called, the `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#fallbackOnBackPressed()` set by the constructor will be triggered.

### setOnBackInvokedDispatcher

Added in [1.6.0](https://developer.android.com/jetpack/androidx/releases/activity#1.6.0)

```
@RequiresApi(value = 33)
public final void setOnBackInvokedDispatcher(@NonNull OnBackInvokedDispatcher invoker)
```

Sets the `https://developer.android.com/reference/android/window/OnBackInvokedDispatcher.html` for handling system back for Android SDK T+.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/window/OnBackInvokedDispatcher.html invoker` | the OnBackInvokedDispatcher to be set on this dispatcher |

## Extension functions

### OnBackPressedDispatcherKt.addCallback

Artifact: [androidx.activity:activity](https://developer.android.com/jetpack/androidx/releases/activity) [View Source](https://cs.android.com/search?q=file:androidx/activity/OnBackPressedDispatcher.kt+function:addCallback)

```
public final @NonNull OnBackPressedCallback OnBackPressedDispatcherKt.addCallback(
    @NonNull OnBackPressedDispatcher receiver,
    LifecycleOwner owner,
    boolean enabled,
    @NonNull Function1<@NonNull OnBackPressedCallback, Unit> onBackPressed
)
```

Creates and registers a new `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback` that invokes `https://developer.android.com/reference/androidx/activity/package-summary#(androidx.activity.OnBackPressedDispatcher).addCallback(androidx.lifecycle.LifecycleOwner,kotlin.Boolean,kotlin.Function1)`.

If a `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` is provided, the callback's enabled state automatically follows the lifecycle: it is enabled while the lifecycle is at least `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#STARTED` and disabled otherwise. The callback stays registered until the `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` is destroyed.

A default `https://developer.android.com/reference/androidx/activity/package-summary#(androidx.activity.OnBackPressedDispatcher).addCallback(androidx.lifecycle.LifecycleOwner,kotlin.Boolean,kotlin.Function1)` state can be supplied.

## Legacy Behavior

To restore the legacy add/remove behavior, set `https://developer.android.com/reference/androidx/activity/ActivityFlags#isOnBackPressedLifecycleOrderMaintained()` to `false`. In legacy mode, the handler is added on `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_START` and removed on `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_STOP`, which may change dispatch ordering across lifecycle transitions.