Open In App

How to Use Predictive Back Gesture in Android 13?

Last Updated : 26 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A predictive back gesture is now available for Android devices with phones, big screens, and foldable in Android 13 (API level 33). This feature, which is a part of a multi-year release, will allow users to preview the destination or another outcome of a back gesture before fully completing it, giving them the option to continue or stay in the current display. As you have worked on previous android versions you will be able to add a predictive back gesture to your android app by the end of this article, you already know how to currently customize the onBackPressed method to add custom back navigation to your Android App. However, you will only be able to test this out once the final version of Android 13 is released to the public.

For instance, as seen in the mockup in figure 1, utilizing a back gesture can show an animated preview of the Home screen behind your app. By turning on a developer option, Android 13 users can test this back-to-home animation (as described on this page).

If your app uses native back methods:

If your app doesn’t use any customized back behavior, updating it to enable this feature is simple (in other words, it leaves back handling up to the system). Simply select the feature and opt-in as instructed on this page a little bit later. The most typical use case is this one (and the most recommended). It applies to new or current apps that use OnBackPressedDispatcher to handle custom gesture navigation as explained in Provide custom back navigation. 

Step #1: Upgrade to AndroidX Activity 1.6.0-alpha05 to guarantee that APIs (such as Fragments and the Navigation Component) already using OnBackPressedDispatcher APIs function flawlessly with the predictive back gesture.

// Your app's gradle file
dependencies {
    implementation "androidx.activity:activity:1.6.0-alpha05"
}

And this is simply what you need to add a predictive back gesture in your Android App, using the default Android behavior is too easy and implemented too easily.

If your app uses custom back methods:

Depending on whether your app supports AndroidX and how it handles back navigation, there are several migration paths if it implements bespoke back behavior.

Particulars

Handling of Back Navigation

Details

The app is using AndroidX Libraries Android X API Use the method mentioned above to handle the backPress
The app is not using the AndroidX Libs Unsupported platform APIs with migration potential Do not opt-in until Android makes it required.

GeekTip: If you don’t upgrade your app by the major Android release that comes after 13 by then, users will have issues with your app’s Back navigation.

Depending if your application is using a custom back navigation then you need to follow these simple steps, and you still need to try the predictive back gesture, continue following this article and keep going to add the predictive back gesture.

Step #1: Implement OnBackPressedCallback to move your system’s Back handling functionality to AndroidX’s OnBackPressedDispatcher. For this refer to the Geeks for Geeks article on how to use onBackPressed methods.

Step #2: When you’re ready to stop capturing the back gesture, disable the OnBackPressedCallback.

Step #3: Stop intercepting back events with KeyEvent.KEYCODE BACK or OnBackPressed.

Upgrade to AndroidX Activity 1.6.0-alpha05 as soon as possible:

// Your app's gradle file
dependencies {
    implementation "androidx.activity:activity:1.6.0-alpha05"
}

This way you can implement backPressed on your application, then you can continue your app’s other method as you need to implement, this will add a beautiful effect to your Android Application.

To transition unsupported APIs to the platform API, do the following steps:

On devices running Android 13 or higher, use the new OnBackInvokedCallback API; on devices running Android 12 or lower, rely on the unsupported APIs.

  • With onBackInvokedDispatcher, register your own back logic in OnBackInvokedCallback. By doing this, the current activity is kept from being completed, and once the user has done using the system Back navigation, your callback has a chance to respond to the Back action.
  • When you’re ready to cease intercepting the back gesture, deactivate the OnBackInvokedCallback. Otherwise, users that utilize the system Back navigation may experience unfavorable behavior, such as “being stuck” between views and being forced to force-quit their app.

Java




@Override void onCreate()
{
    if (BuildCompat.isAtLeastT()) {
        getOnBackInvokedDispatcher()
            .registerOnBackInvokedCallback(
                OnBackInvokedDispatcher.PRIORITY_DEFAULT,
                ()
                    -> {
                        // Geeks for Geeks Android Tutorials
                        // Use this space to add your
                        // onBackPressMethod
                        // Note the priority to close your
                        // application is default as
                        // mentioned.
                    });
    }
}


Conclusion

This is how you will be implementing the predictive back gesture in your Android Application, by this you can utilize the transition and tell your users what will happen when they BackPress on your android app, or swipe using the back gesture. Also notice that you will only be able to check this on your current application only when the final beta for Android 13 is released, as this will be a beta feature and will only be visible in ‘Developer Options’ at a later time. Once the update is pushed from the Google Side of the System you will then be able to view your predictive back press by:

  1. Navigate to Settings > System > Developer options on your smartphone.
  2. Predictive back animations should be chosen.
  3. To see it in action, open your upgraded app and utilize the back gesture.

Hope this article helped you to add the predictive back navigation to your Android App, and that you use them to create an even better Android App for further Android releases when this comes out. of the beta.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads