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?
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.
Now to migrate the above code, we have to follow 2 Simple Steps…
Step 1 :
Create a onBackPressed Callback.
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) }
Job Offers
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. 🙂
If you learnt something new, Please don’t forget to Follow, like, and support me 👍😊
This article was originally published on proandroiddev.com on November 26, 2022