# OnBackPressedCallback

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

*** ** * ** ***


```
public abstract class OnBackPressedCallback
```

<br />

*** ** * ** ***

Class for handling `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#onBackPressed()` callbacks without strongly coupling that implementation to a subclass of `https://developer.android.com/reference/androidx/activity/ComponentActivity`.

This class maintains its own `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#isEnabled()`. Only when this callback is enabled will it receive callbacks to `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#handleOnBackPressed()`.

Note that the enabled state is an additional layer on top of the `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` passed to `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#addCallback(androidx.activity.OnBackPressedCallback)` which controls when the callback is added and removed to the dispatcher.

By calling `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#remove()`, this callback will be removed from any `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher` it has been added to. It is strongly recommended to instead disable this callback to handle temporary changes in state.

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

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#OnBackPressedCallback(kotlin.Boolean)(boolean enabled)` |

| ### Public methods |
|---|---|
| `void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#handleOnBackCancelled()()` Callback for handling the system UI generated equivalent to `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#dispatchOnBackCancelled()`. |
| `abstract void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#handleOnBackPressed()()` Callback for handling the `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#onBackPressed()` event. |
| `void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#handleOnBackProgressed(androidx.activity.BackEventCompat)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/BackEventCompat backEvent)` Callback for handling the system UI generated equivalent to `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#dispatchOnBackProgressed(androidx.activity.BackEventCompat)`. |
| `void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#handleOnBackStarted(androidx.activity.BackEventCompat)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/BackEventCompat backEvent)` Callback for handling the system UI generated equivalent to `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#dispatchOnBackStarted(androidx.activity.BackEventCompat)`. |
| `final boolean` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#isEnabled()()` The enabled state of the callback. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#remove()()` Removes this callback from any `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher` it is currently added to. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#setEnabled(kotlin.Boolean)(boolean value)` The enabled state of the callback. |

## Public constructors

### OnBackPressedCallback

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

```
public OnBackPressedCallback(boolean enabled)
```

| Parameters |
|---|---|
| `boolean enabled` | The default enabled state for this callback. |

## Public methods

### handleOnBackCancelled

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

```
@MainThread
public void handleOnBackCancelled()
```

Callback for handling the system UI generated equivalent to `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#dispatchOnBackCancelled()`.

This will only be called by the framework on API 34 and above.

### handleOnBackPressed

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

```
@MainThread
public abstract void handleOnBackPressed()
```

Callback for handling the `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#onBackPressed()` event.

### handleOnBackProgressed

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

```
@MainThread
public void handleOnBackProgressed(@NonNull BackEventCompat backEvent)
```

Callback for handling the system UI generated equivalent to `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#dispatchOnBackProgressed(androidx.activity.BackEventCompat)`.

This will only be called by the framework on API 34 and above.

### handleOnBackStarted

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

```
@MainThread
public void handleOnBackStarted(@NonNull BackEventCompat backEvent)
```

Callback for handling the system UI generated equivalent to `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher#dispatchOnBackStarted(androidx.activity.BackEventCompat)`.

This will only be called by the framework on API 34 and above.

### isEnabled

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

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

The enabled state of the callback. Only when this callback is enabled will it receive callbacks to `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#handleOnBackPressed()`.

When registered with a `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner`, the callback is only active when **both** this property is `true` and the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` is at least `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#STARTED`.

### remove

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

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

Removes this callback from any `https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher` it is currently added to.

### setEnabled

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

```
@MainThread
public final void setEnabled(boolean value)
```

The enabled state of the callback. Only when this callback is enabled will it receive callbacks to `https://developer.android.com/reference/androidx/activity/OnBackPressedCallback#handleOnBackPressed()`.

When registered with a `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner`, the callback is only active when **both** this property is `true` and the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` is at least `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#STARTED`.