OnBackPressed deprecated So, let’s use modern way to handle it
If you have updated any application to targetSdk 33, you probably have seen that OnBackPressed() is deprecated in Activities. Hello! Developers, My name is Anubhav Sharma Today I am going to use modern way to handle back press event.
Before we start, let’s talk about why there is such a change. You may know that starting from Android 10 the system provide gesture navigation feature. In a bit more detail, the system supports gestures like swiping left to right to navigate back ( just like IOS devices ). But this has led unexpected behaviour when it has been combined with horizontal swipes in applications. The users reported that they sometimes trigger system back while swiping horizontally in an application.
The problem is that the android system cannot differentiate if the gesture is for system back or for application back navigation. In other words, It cannot tell if the application handles the gesture.
So should I use instead ?
onBackPressedDispatcher
comes into play now. It is stated as
Dispatcher that can be used to register OnBackPressedCallback instances for handling the ComponentActivity.onBackPressed() callback via composition.
To trigger onBackPressed feature, using onBackPressedDispatcher.onBackPressed()
should be enough. If you are using ComponentActivity
old onBackPressed method should be triggered unless you provide an OnBackPressedCallback
.
Speaking of OnBackPressedCallback
, the callback is our new way to detect back gesture/navigation. When user taps back button or performs a navigate back gesture (if enabled). OnBackPressedDispatcher
invokes the callback when needed and the callback is enabled.
As you can see above the callback a boolean value as parameter that is used to determine if the callback is enabled by default. After in your code you can toggle the callback between enabled/disabled states. When the callback is enabled the handleOnBackPressed method is invoked whenever there is an back navigation event from the user. If the callback is disabled the callback is not triggered, obviously but instead the Android system will handle the event.
Starting from Android 13 the system will provide predictive back gesture which shows users that they are about to navigate back to launcher from the application with swipe back action, so that users can decide whether or not they really want to go back to launcher. For this upcoming future, the application should tell the system that if it handles swipe back gesture or not.
How to implement it
To handle the swipe back gestures and notify the system about it, you need to follow these steps.
1.Add implementation"androidx.activity:activity:1.5.1"as a dependency to your modules build.gradle file2.Update your project to target api 33 by usingcompileSdk 33 and targetSdk 333.enable view binding buildFeatures { viewBinding = true }
Just copy and paste the callback for your use
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
handleOnBack()
}
private fun handleOnBack() {
onBackPressedDispatcher.addCallback(
this@MainActivity, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
when {
isEnabled -> {
Snackbar.make(
binding.root,
"OnBackPressed",
Snackbar.LENGTH_LONG
).show()
isEnabled = false
}
else -> {
isEnabled = false
finish()
}
}
}
})
}
}
By registering the callback the Android system will know that your application will handle the event and the system will not perform any predictive back gesture animation.
I’ve created a sample project for this topics you can check it out if wish.