Migrate The Deprecated OnBackPressed Function — Android 13
While Targeting Android 13 , OnbackPressed Override Function is deprecated😢. Usually, we used to intercept on backpress and add animations. we all know, this onBackPressed function will call as soon as the Back button is clicked (Hardware) or back pressed from the navigation gesture. But, now it’s time to change/move….
Hmm., No matter what “Change is the only constant..!!”

Why is the OnBackPress function Deprecated?🤔
In Android 13, we have a new Preview feature called, “Predictive back gesture.” Currently, it’s not available for end-Users. Because of this feature, they deprecated the onBackPressed Function in Android 13. So we are going to migrate and make our apps to get ready for Android 13’s predictive back gesture.
Note: As Android Team mentioned in the note, Predictive back gesture it’s only visible for developers for testing purposes while Targeting Android 13.
Now, its time to Migrate.. 🤞
Let’s see our old way of usage,
override fun onBackPressed() {
//showing dialog and then closing the application..
showDialog()
}
private fun showDialog(){
MaterialAlertDialogBuilder(this).apply {
setTitle("are you sure?")
setMessage("want to close the application ?")
setPositiveButton("Yes") { _, _ -> finish() }
setNegativeButton("No", null)
show()
}
}
The above code will show the popup when back press is triggered. If the user clicks on yes, the app will close. If the user clicks on No, the app will not close. Pretty simple example right?
Now to migrate the above code, we have to follow 2 Simple Steps…
Step 1 :
To enable the predictive back gesture APIs, set enableOnBackInvokedCallback to true in the manifest.
<application
...
android:enableOnBackInvokedCallback="true" // Enables this feature.
... >
...
</application>
Don’t worry about warning, it doesn’t affect our app or flow. Simply that line of code in AndroidManifest.xml will run only on Android 13 and above.
Then, we want to Create a onBackPressed Callback in Activity / Fragment.
OnBackPressedCallback(enabled) //enabled is true or false
In this callback, enabled is a default parameter which we have to pass by any condition if needed. If you want by default, you can enable it by passing true.
The below code is the total replica of our old way onbackPressed() function.
private val onBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
//showing dialog and then closing the application..
showDialog()
}
}
STEP 2 :
Add the callback in the onBackPressedDispatcher(). This should be inside the onCreate function and it’s a good practice to use the bottom of setContentView or after Binding the layout.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// adding onbackpressed callback listener.
onBackPressedDispatcher.addCallback(this,onBackPressedCallback)
}
Final MainActivity code :
class MainActivity : AppCompatActivity() {
private val onBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
//showing dialog and then closing the application..
showDialog()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// adding onbackpressed callback listener.
onBackPressedDispatcher.addCallback(this,onBackPressedCallback)
}
private fun showDialog(){
MaterialAlertDialogBuilder(this).apply {
setTitle("are you sure?")
setMessage("want to close the application ?")
setPositiveButton("Yes") { _, _ -> finish() }
setNegativeButton("No", null)
show()
}
}
}
Note : If suppose onBackPressedDispatcher or OnBackPressedCallback are not found for you guys, please check your activity ktx version which should be at least or more than 1.6.1.
If you create a new project in Android studio, the above Note is not mandatory.
dependencies { //build.gradle app level
implementation 'androidx.activity:activity-ktx:1.6.1'
}
That’s it..! now we replaced the deprecated onBackPressed function. :)
Now, if you not have something like Dialog box for confirmation in onBackPressed, you can get back-to-home gesture animations , which is in Android 13.
To test it, make onBackPressedCallback isEnabled as false.(This will not show Dialog popup or anything inside the callback function, typically you can add this isEnabled = false under some conditions.)
make onBackPressedCallback isEnabled as false .(Note this will not show Dialog popup.)
And then, Starting with the Android 13 final release, you should be able to enable a developer option to test the back-to-home animation
- On your device, go to Settings > System > Developer options.
- Select Predictive back animations.
- Launch your updated app, and use the back gesture to see it in action.
That’s it!!
If you learnt something new, Please don’t forget to Follow, like, and support me 👍😊
If you learnt something new, Please don’t forget to Follow, like, and support me 👍😊