Prepare your app to support predictive gestures

Prepare your app to support predictive gestures


Posted by Jason Tang, Product Management, Diego Zuluaga, Developer Relations, and Michael Mauzy, Developer Documentation

Ever since we introduced gesture navigation in Android 10, users have expressed their desire to know where a gesture back takes them before they complete it.

As a first step in meeting this need, we have a predictive back gesture. When a user begins their gesture by swiping back, we show an animated preview of the destination UI, and the user can complete the gesture to navigate to that UI if they wish – as shown in the following example.

While the predictive return gesture is not visible to users in Android 13, we are making an early version of the UI available as a developer option for testing from beta 4. We plan to make the UI available to users in the future Android release, and we would like all apps to be ready. We also work with partners to ensure it is consistent across all devices.

Read on to learn more about how to try out and support the new gesture in your apps. Adding predictive gesture support is easy for most apps, and you can get started today.

We also encourage you to feedback.

Try the predictive back gesture in Beta 4

To try out the early version of the predictive return gesture available through the developer option, you’ll need to: update your app first to support the predictive return gesture, and then enable developer option.

Update your app to support predictive gesture back

To make predictive backward gesture useful and consistent for users, we are moving to a forward-looking model for handling back events by add new APIs and deprecate existing APIs.

The new platform APIs and updates for AndroidX Activity 1.6+ are designed to facilitate your transition from unsupported APIs (KeyEvent#KEYCODE_BACK and OnBackPressed) to make the predictive return gesture as smooth as possible.

The new platform APIs include: OnBackInvokedCallback and OnBackInvokedDispatcher, that supports AndroidX Activity 1.6+ through the existing OnBackPressedCallback and OnBackPressedDispatcher APIs.

You can test this feature in two to four steps, depending on your existing implementation.

To start testing this feature:

1. Upgrade to AndroidX Activity 1.6.0-alpha05. By upgrading your dependency on AndroidX Activity, APIs that already use the OnBackPressedDispatcher APIs such as Fragments and the navigation component work seamlessly when you opt in to the predictive return gesture.

// In your build.gradle file:
dependencies {

// Add this in addition to your other dependencies
implementation “androidx.activity:activity:1.6.0-alpha05”

2. Sign up for the predictive back gesture. Register your app by the EnableOnBackInvokedCallback flag to true at the application level in the AndroidManifest.xml.

android:enableOnBackInvokedCallback=”true”

… >

If your app doesn’t intercept the backup event, you’re done with this step.

Remark: Opt-in is optional in Android 13 and will be ignored after this version.

3. Create a callback request to intercept the system’s back button/event. If possible, we recommend using the AndroidX APIs as shown below. For non-AndroidX use cases, check the platform API mentioned above.

This snippet implements handleOnBackPressed and adds the OnBackPressedCallback to the OnBackPressedDispatcher at activity level.

fall onBackPressedCallback = object: OnBackPressedCallback(WHERE) {

Overwrite pleasure handleOnBackPressed() {

// Your business logic to handle the pushed back event

}

}

requiredActivity().onBackPressedDispatcher

.addCallback(onBackPressedCallback)

4. When your app is ready to stop intercepting the system backup event, disable the onBackPressedCallback callback.

onBackPressedCallback.isEnabled = webView.canGoBack()

Remark: Your app may require the use of the platform APIs (OnBackInvokedCallback and OnBackPressedDispatcher) to implement the predictive return gesture. Read our documentation for details.

Enable the developer option to test the predictive back gesture

After you update your app to support the predictive back gesture, you can enable a developer option (supported in Android 13 Beta 4 and above) to see for yourself.

To test this animation, complete the following steps:

  1. On your device, go to Settings > System > Developer Options.
  2. Select Predictive back animations.
  3. Launch your updated app and use the back gesture to see it in action.

Thanks again for all feedback and being part of the Android community, we love working together to provide the best experience for our users.