# OnBackPressedDispatcherKt

Added in 1.7.0

*** ** * ** ***


```
public final class OnBackPressedDispatcherKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static 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#(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 methods

### 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) Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/activity#1.7.0)

```
public static final @NonNull OnBackPressedCallback 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.