OnBackPressed Deprecated So What to Use

Oğuzhan Aslan
4 min readJul 30, 2022

--

Photo by Nick Fewings on Unsplash

If you have updated any application to targetSdk 33, you probably have seen that OnBackPressed() is deprecated in Activities.

Depreciation of “OnBackPressed()”

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.6.0-alpha05"as a dependency to your modules build.gradle file
  2. Update your project to target api 33 by usingcompileSdk 33 and targetSdk 33
  3. Add android:enableOnBackInvokedCallBack="true” to your applications manifest <application> tag

After doing this your application should not intercept back gesture.

Also you need to enable the feature from developer options, you can do it by following these.

  1. Settings -> System -> Developer options.
  2. Select Predictive back animations.

After that the device/emulator is ready to show predictive back gesture animations.

If you added the OnBackPressedCallback to onBackPressedDispatcher , your application will handle the back button clicks and onBackPressedDispatcher.onBackPressed() invokes but not swipe back gestures. To rephrase it, the application still be doing the predictive back gesture animations even if the OnBackPressedCallback isEnabled.

You should use OnBackInvokedCallback to get the callback for the go back to launcher events. Similar to the other callback OnBackInvokedDispatcher handles the callback and invokes it when needed, you just need to register your callback and unregister when you are stopped handling the event.

Note that you cannot trigger the callback programmatically unlike OnBackPressedCallback . This is because the callback related with the system not with our application directly. Thus, your application can just listen for the event.

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.

That is it from me for now. Please do not hesitate to ask your questions. Also there is more detail about both these topics your can take a look if you wish, I attach my resources below.

Do not forget to follow me for upcoming blogs/articles.

LinkedIn

Resources

https://developer.android.com/about/versions/13/features/predictive-back-gesture

https://developer.android.com/codelabs/gesture-navigation?hl=en#0

https://developer.android.com/reference/android/window/OnBackInvokedDispatcher

Love you all.

Stay tune for upcoming blogs.

Take care.

--

--