The Shift to Predictive Navigation
For years, Android developers relied on overriding onBackPressed() to handle custom navigation logic, such as showing a 'Save Changes' dialog before exiting a screen. However, this approach creates a technical bottleneck. Because the system has to wait for the app to execute its code, it cannot show the 'Predictive Back' animation that lets users peek at the home screen during a swipe gesture.
Starting with Android 13 and becoming standard in Android 14, Google introduced the OnBackInvokedCallback. This API tells the system ahead of time whether the app intends to handle the back event, allowing for smooth, system-managed transitions.
Step 1: Enabling the Feature
Before writing any Kotlin code, you must explicitly tell the Android OS that your app is ready for predictive back. Open your AndroidManifest.xml and add the following attribute to your <application> tag:
<application
android:enableOnBackInvokedCallback="true"
... >
</application>
Without this flag, the system will continue to use the legacy back behavior, and your new callbacks will never be triggered.
Step 2: Registering the Callback
Unlike the old method where you simply override a function, the new API requires you to register and unregister a callback. This is more efficient because you can enable or disable the back handling based on the UI state (e.g., only when a modal is open).
Here is how you implement it within an Activity:
import android.window.OnBackInvokedCallback
import android.window.OnBackInvokedDispatcher
import android.os.Build
class DetailActivity : AppCompatActivity() {
private val backCallback = OnBackInvokedCallback {
// Your custom logic here
showExitConfirmationDialog()
}
override fun onStart() {
super.onStart()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
onBackInvokedDispatcher.registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT,
backCallback
)
}
}
override fun onStop() {
super.onStop()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
onBackInvokedDispatcher.unregisterOnBackInvokedCallback(backCallback)
}
}
private fun showExitConfirmationDialog() {
// Handle the actual back action
finish()
}
}
Understanding Priorities
The registerOnBackInvokedCallback method takes two parameters: priority and the callback itself. PRIORITY_DEFAULT is standard for most screens. However, if you are building a specialized UI component like a picture-in-picture overlay or a custom navigation drawer that must intercept the back gesture before the main activity logic, you can use PRIORITY_OVERLAY.
Why This Matters for SEO and UX
Modern Android users expect fluid, gesture-based navigation. Apps that stick to legacy back button logic feel clunky and 'jittery' on newer devices. By adopting OnBackInvokedCallback, you ensure your app feels native to the OS. From a development standpoint, this decoupling of the back event from the UI thread prevents 'Application Not Responding' (ANR) errors that used to occur when heavy logic was placed inside the old onBackPressed().
If you are using fragments, ensure you use the OnBackPressedDispatcher from the androidx.activity library, as it has been updated to handle these system-level changes internally, providing a backward-compatible bridge for older Android versions.




