onBackPressed() is crossed out in Android Studio: what to use instead
'onBackPressed()' is deprecatedShort 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.
Fix it fast
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 debugRuntimeClasspath2Match what you see, then jump to its fix
- 1
override fun onBackPressed() { ... } in an Activity classAn Activity overrides onBackPressedFix - 2
The module resolves an androidx.activity version below 1.6.0The module uses an older androidx.activityFix - 3
Back keeps running fragment logic after the fragment is goneA Fragment handles back and the callback leaks or fires too longFix - 4
A Compose screen still uses an activity-level overrideBack handling in Jetpack ComposeFix
- 1
What does the error look like?
'onBackPressed()' is deprecatedBack 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/main2Fix
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 installDebugConfirmed by developer.android.com
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.activity2Fix
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.activityConfirmed by developer.android.com
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/main2Fix
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 installDebugConfirmed by developer.android.com
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/main2Fix
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 installDebugConfirmed by developer.android.com
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:
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Nuclear Home VideoA video-streaming Android app built from scratch, engineered for smooth playback.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Mobile apps
Android build fails because a product flavor has no flavor dimension
The flavor 'flavor_name' is not assigned to a flavor dimension.
Android Gradle plugin refuses to build when a product flavor is not assigned to a flavor dimension.
2 causes, each with a fixMobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
Android build fails because two libraries ship the same META-INF file
More than one file was found with OS independent path
Two dependencies put a file at the same path inside the APK and the plugin will not guess which one wins.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
FirebaseInstanceIdService and FirebaseInstanceId.getToken() no longer work
Deprecation warning on FirebaseInstanceId.getInstance().getToken()
Move token handling into a class that extends FirebaseMessagingService and override onNewToken, which runs when a token is first created and whenever it changes.
2 causes, each with a fix