# 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)

*** ** * ** ***


```
abstract class OnBackPressedCallback
```

<br />

*** ** * ** ***

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

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

Note that the enabled state is an additional layer on top of the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` passed to `https://developer.android.com/reference/kotlin/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/kotlin/androidx/activity/OnBackPressedCallback#remove()`, this callback will be removed from any `https://developer.android.com/reference/kotlin/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/kotlin/androidx/activity/OnBackPressedDispatcher` |   |

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/activity/OnBackPressedCallback#OnBackPressedCallback(kotlin.Boolean)(enabled: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` |

| ### Public functions |
|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/activity/OnBackPressedCallback#handleOnBackCancelled()()` Callback for handling the system UI generated equivalent to `https://developer.android.com/reference/kotlin/androidx/activity/OnBackPressedDispatcher#dispatchOnBackCancelled()`. |
| `abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/activity/OnBackPressedCallback#handleOnBackPressed()()` Callback for handling the `https://developer.android.com/reference/kotlin/androidx/activity/OnBackPressedDispatcher#onBackPressed()` event. |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/activity/OnBackPressedCallback#handleOnBackProgressed(androidx.activity.BackEventCompat)(backEvent: https://developer.android.com/reference/kotlin/androidx/activity/BackEventCompat)` Callback for handling the system UI generated equivalent to `https://developer.android.com/reference/kotlin/androidx/activity/OnBackPressedDispatcher#dispatchOnBackProgressed(androidx.activity.BackEventCompat)`. |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/activity/OnBackPressedCallback#handleOnBackStarted(androidx.activity.BackEventCompat)(backEvent: https://developer.android.com/reference/kotlin/androidx/activity/BackEventCompat)` Callback for handling the system UI generated equivalent to `https://developer.android.com/reference/kotlin/androidx/activity/OnBackPressedDispatcher#dispatchOnBackStarted(androidx.activity.BackEventCompat)`. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/activity/OnBackPressedCallback#remove()()` Removes this callback from any `https://developer.android.com/reference/kotlin/androidx/activity/OnBackPressedDispatcher` it is currently added to. |

| ### Public properties |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/activity/OnBackPressedCallback#isEnabled()` 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)

```
OnBackPressedCallback(enabled: Boolean)
```

| Parameters |
|---|---|
| `enabled: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | The default enabled state for this callback. |

## Public functions

### handleOnBackCancelled

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

```
@MainThread
open fun handleOnBackCancelled(): Unit
```

Callback for handling the system UI generated equivalent to `https://developer.android.com/reference/kotlin/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
abstract fun handleOnBackPressed(): Unit
```

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

### handleOnBackProgressed

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

```
@MainThread
open fun handleOnBackProgressed(backEvent: BackEventCompat): Unit
```

Callback for handling the system UI generated equivalent to `https://developer.android.com/reference/kotlin/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
open fun handleOnBackStarted(backEvent: BackEventCompat): Unit
```

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

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

### remove

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

```
@MainThread
fun remove(): Unit
```

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

## Public properties

### isEnabled

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

```
var isEnabled: Boolean
```

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

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