avatarYanneck Reiß

Summary

This article discusses the migration process from the deprecated onBackPressed() function to the OnBackPressedCallback in Android 13 to support the predictive back gesture feature.

Abstract

The article explains that starting with Android 13, the onBackPressed() function has been marked as deprecated. The OnBackPressedDispatcher from the androidx.activity library now integrates the OnBackInvokedCallback logic required for supporting the predictive back gesture feature. The article provides an example of migrating the legacy version of handling a back press event to the new OnBackPressedCallback method, demonstrating how to use the OnBackPressedDispatcher with a LifecycleOwner like an Activity or Fragment. The article concludes by stating that the migration is easy to handle and now requires the same logic for implementing back-pressed interception in Fragments.

Bullet points

  • onBackPressed() function has been deprecated in Android 13.
  • OnBackPressedDispatcher from androidx.activity library integrates OnBackInvokedCallback logic for predictive back gesture support.
  • Migration example from legacy back press event handling to OnBackPressedCallback.
  • Usage of OnBackPressedDispatcher with a LifecycleOwner like an Activity or Fragment.
  • Migration is easy to handle and requires the same logic for back-pressed interception in Fragments.

How To Migrate The Deprecated OnBackPressed Function

Get ready for Android 13’s predictive back gesture

Original photo by Tim Douglas from Pexels

Starting with Android 13 (API Level 33), the onBackPressed() function has been marked as deprecated.

If you visit the official documentation of onBackPressed(), you will see the note “Starting from Android 13 (API level 33), back event handling is moving to an ahead-of-time model and Activity#onBackPressed() and KeyEvent#KEYCODE_BACK should not be used to handle back events (back gesture or back button click)”.

But why is that and what does that mean for our Android apps and how can we replace the deprecated parts? These questions will be answered in this article.

This article is also available as a YouTube video

Predictive Back Gesture

The predictive back gesture is a part of Android 13. However, at the time of writing, Android 13 has already been released, but the predictive back gesture is still in preview and will be taking part in a later partial release as soon as it is fully implemented.

The predictive back gesture is a new feature that is supposed to allow you to preview the last screen before actually navigating back to it.

Just to mention, even if it’s not actually part of the article, in order to preview the feature, you can go to Settings > System > Developer options and activate the Predictive back animations.

Note that you will also need to add the following to your app’s AndroidManifest.xml :

However, to actually support this feature, we also need to migrate the way we implement our custom back navigation interceptions.

OnBackPressedDispatcher

Meanwhile the androidx.activity library’s OnBackPressedDispatcher has integrated the OnBackInvokedCallback logic that is required in order to support the feature.

If we take a glance at the source code of the library. We can verify that the OnBackPressedDispatcher contains the respective code for Android 13 and above in which the onBackPressedInvoked callback gets registered:

This is very helpful because we don’t need to introduce OS checks or need to add other duplicated code.

We can just use the OnBackPressDispatcher that is part of the ComponentActivity which is on the other hand the base class of all the basic Activity variations.

Before we proceed, make sure to add the following dependency to get access to it:

Migrate To OnBackPressedCallback

Let’s take an everyday use case as an example in which you want to intercept the onBackPressed call. Think about the case where you want to avoid the user accidentally exiting the app via back press or in respective via a swipe gesture.

The legacy version

We can handle this case by handling this situation by ourselves and showing an alert with a warning message instead of directly passing the back press event along.

If the user confirms the dialog, we could then just finish our respective Activity.

Take a look at the following code snippet that shows a very simplified version of this use case with a single activity and no further logic.

The result can be seen in the gif below:

Small animation that shows the use case’s flow

The migration

Okay, now that we talked about the use case and the legacy implementation: How can we actually get rid of the deprecated code?

The OnBackPressedDispatcher is a class that allows you to register a OnBackPressedCallback to a LifecycleOwner. A LifecycleOwner is for example an Activity.

You could also register such a callback from a Fragment and set the Fragment as the LifecycleOwner.

The callback will only actually be added when the respective Lifecycle.State reaches started. However, as soon as the respective LifecycleOwner comes into the destroyed state, the callback will automatically be deregistered.

Take a look at the following code that shows the migrated version of the previously shown SampleActivity.kt:

As you can see we declare a new variable onBackPressedCallback and initialize it with an object expression with the supertype OnBackPressedCallback.

In that declaration, we override the handleOnBackPressed() function that is the equivalent to the deprecated onBackPressed() function.

That said, we can now simply call the showAppClosingDialog() function and the code will behave exactly the same.

Note that we also need to pass in a boolean into the constructor of the OnBackPressedCallback that determines if the callback is currently enabled.

This parameter allows you to dynamically enable or disable the callback.

As the last thing to discuss, let’s take a look at the onCreate(..) function. Here we actually add the callback to the OnBackPressedDispatcher that is part of the ComponentActivity which is on the other hand the supertype for various Activity classes like the used AppCompatActivity.

As already mentioned, we also need to add the LifecycleOwner when adding the callback. Because we want to callback to be bound to our Activity, we just pass in this as the argument to reference it.

Conclusion

In this article, we saw how we can migrate the deprecated onBackPressed() function to the OnBackPressedCallback.

We also discussed that the function got deprecated in order to support the upcoming predictive back gesture for Android 13 devices.

In my opinion, migration is very easy to handle, and now just forces us to use the same logic that we already needed when we wanted to implement back-pressed interception in our Fragments.

I hope you had some takeaways, clap if you liked my article, make sure to subscribe to get notified via e-mail, and follow for more!

Android App Development
AndroidDev
Android
Onbackpressed
Programming
Recommended from ReadMedium