BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Android Adds Experimental Support for Predictive Back Gestures

Android Adds Experimental Support for Predictive Back Gestures

To help users understand where a back gesture will land them in an Android app, Google has introduced a new predictive back gesture. The new feature allows the user to decide whether they want to complete the gesture or not by showing an animated preview of the gesture outcome. It is available in the last Android 13 beta for developers to start experimenting with it and prepare the migration.

Gesture controls were added to Android 10 as a extension to the button-based design used for back actions. While they represented a powerful enhancement to Android user experience, the variety of actions the new paradigm supported could be hard to handle. In particular, Google says, users have expressed a preference for knowing where a back gesture will take them before they complete it.

The new predictive back gesture will not be available to users in the upcoming Android 13 release. Instead, Google is giving developers plenty of time to adapt their apps and adopt the new API. Failing that, Google says, users will experience a broken back gesture navigation. In fact, alongside with the introduction of the new API, Google will remove the deprecated KeyEvent.KEYCODE_BACK event and the onBackPressed method used in Activity and Dialog.

Google offers developers support to make the migration easier through the latest AndroidX Activity 1.6.0-alpha05 release. Non-AndroidX based apps will have to tap directly into the platform API, instead.

In the simplest case, that is when an app uses default back navigation; developers are only required to opt in to the new mechanism by adding android:enableOnBackInvokedCallback="true" to your AndroidManifest.xml.

For apps that handle by themselves the back action to provide a customized experience through OnBackPressedDispatcher, developers will also ensure they use AndroidX Activity 1.6.0-alpha05 for the predictive back gesture to work seamlessly.

On the other end, apps still using the old back gesture mechanism will need to update to AndroidX's OnBackPressedDispatcher and implement OnBackPressedCallback as shown in the following snippet:

 val onBackPressedCallback = object: OnBackPressedCallback(true) {
   override fun handleOnBackPressed() {
     // Your business logic to handle the back pressed event
   }
 }
 requireActivity().onBackPressedDispatcher
   .addCallback(onBackPressedCallback)

The OnBackPressedCallback can be disabled by setting onBackPressedCallback.isEnabled when an app has no defined behaviour for the back gesture.

If an app cannot update to AndroidX for whatever reason, Google is providing a path forward exposing the lower-level OnBackInvokedCallback API. In this case, apps will need to implement an onBackInvoked method to leverage their previous onBackPressed logic and to get a chance to react to the back action once the user completes it. Additionally, they will need to unregister the OnBackInvokedCallback to prevent broken behaviour.

Once developers have updated their apps to the new predictive back gesture mechanism, they will be able to test it by enabling a specific developer option available in Android 13 beta 4 under Settings > System > Developer options.

About the Author

Rate this Article

Adoption
Style

BT