Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

onBackPressed() is crossed out in Android Studio: what to use instead

Error'onBackPressed()' is deprecated

Short answer

Android now routes the Back button and the predictive back gesture through OnBackPressedDispatcher. Stop overriding onBackPressed. Register an OnBackPressedCallback in onCreate with addCallback, use BackHandler in Compose, and use androidx.activity 1.6.0 or higher. Disable the callback when you want the default back behavior.

  • Applies to androidx.activity 1.6.0 or higher, Android 13 to 17, Kotlin 2.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which androidx.activity version the module resolves to. The dispatcher API below needs 1.6.0 or higher.

    ./gradlew :app:dependencyInsight --dependency androidx.activity:activity --configuration debugRuntimeClasspath
  2. 2Match what you see, then jump to its fix

    1. 1override fun onBackPressed() { ... } in an Activity classAn Activity overrides onBackPressedFix
    2. 2The module resolves an androidx.activity version below 1.6.0The module uses an older androidx.activityFix
    3. 3Back keeps running fragment logic after the fragment is goneA Fragment handles back and the callback leaks or fires too longFix
    4. 4A Compose screen still uses an activity-level overrideBack handling in Jetpack ComposeFix

What does the error look like?

  • 'onBackPressed()' is deprecated
  • Back gesture ignores your custom onBackPressed code

Cause 1 of 4

An Activity overrides onBackPressed

1Check

Search the project for the override. Intercepting back through onBackPressed or KeyEvent.KEYCODE_BACK is the unsupported path the Android docs tell you to replace.

grep -rn "onBackPressed\|KEYCODE_BACK" app/src/main

2Fix

Delete the override and add a callback to the activity's dispatcher in onCreate. The callback runs only while it is enabled.

// Kotlin
onBackPressedDispatcher.addCallback(this) {
    // your back logic
    // to hand control back to the system: isEnabled = false; onBackPressedDispatcher.onBackPressed()
}

// Java
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
    @Override
    public void handleOnBackPressed() {
        // your back logic
    }
});

3Verify

Press Back and swipe back on a device. Your code runs, and with the callback disabled the screen closes as normal.

./gradlew installDebug

Confirmed by developer.android.com

Did this fix it?

Cause 2 of 4

The module uses an older androidx.activity

1Check

Predictive back support needs androidx.activity 1.6.0 or higher. Fragments and the Navigation component also need it.

./gradlew :app:dependencies --configuration debugRuntimeClasspath | grep androidx.activity

2Fix

Add or raise the dependency.

dependencies {
    implementation("androidx.activity:activity:1.6.0")
}

3Verify

Sync and rebuild; the resolved version is 1.6.0 or higher.

./gradlew :app:dependencies --configuration debugRuntimeClasspath | grep androidx.activity

Confirmed by developer.android.com

Did this fix it?

Cause 3 of 4

A Fragment handles back and the callback leaks or fires too long

1Check

Use addCallback with a LifecycleOwner. The callback is active only while that owner is at least started, and it is removed when the owner is destroyed.

grep -rn "addCallback" app/src/main

2Fix

Register the callback from the fragment with the fragment as lifecycle owner.

class MyFragment : Fragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requireActivity().onBackPressedDispatcher.addCallback(this) {
            // handle back for this fragment
        }
    }
}

3Verify

Navigate away from the fragment and press Back; its callback no longer runs.

./gradlew installDebug

Confirmed by developer.android.com

Did this fix it?

Cause 4 of 4

Back handling in Jetpack Compose

1Check

Compose has its own composable for intercepting back, from androidx.activity:activity-compose.

grep -rn "BackHandler" app/src/main

2Fix

Use BackHandler, enabled only while the screen needs it. Use PredictiveBackHandler if you need the swipe progress.

BackHandler(enabled = hasUnsavedChanges) {
    // show a confirm dialog
}

3Verify

With hasUnsavedChanges true, Back runs your block; with false, the system handles it.

./gradlew installDebug

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Enable a callback only while the screen has something to intercept, and disable it otherwise.
  • Do not use back callbacks to log or run business logic; the Android docs advise other signals for that.
  • Test the back gesture on Android 13 or later, not only the Back button.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.