In android 13, there is list of features and behavior changes that might affect app developers. One of them is predictive back gesture.

Android 13 (API level 33) introduces support for predictive back gesture for Android devices. It is not fully implemented feature it is part of a multi-year release. This feature will help users to preview the destination or other result of a back gesture before fully completing it. It allows them to decide whether to continue or stay in the current view.

Android-14-predictive-back-gesture
Android-14-predictive-back-gesture

Supporting the predictive back gesture requires updating your app, and make it backward compatible. Most apps will use the backward compatible AndroidX API.

To enable this feature you need to upgrade your app to AppCompat 1.6.0-alpha05 (AndroidX) or higher API. If your app fits into this category, follow these steps to add support for the predictive back gesture:

// In your build.gradle file:
dependencies {

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

 

Once you’ve determined how to update your app , it will be easy to opt in to supporting the predictive back gesture.

To opt in, in AndroidManifest.xml, in the tag, set the android:enableOnBackInvokedCallback flag to true.

<application
    ...
    android:enableOnBackInvokedCallback="true"
    ... >
...
</application>

Defaults value of OnBackInvokedCallback to false and does the following:

  1. Disables the predictive back gesture system animation.
  2. Ignores OnBackInvokedCallback, but OnBackPressedCallback calls continue to work.

Java Code for Predictive Back Gesture.

@Override
void onCreate() {
if (BuildCompat.isAtLeastT()) {
getOnBackInvokedDispatcher
().registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT,
() -> {
/**
* onBackPressed logic goes here – For instance:
* Prevents closing the app to go home screen when in the
* middle of entering data to a form
* or from accidentally leaving a fragment with a WebView in it
*
* Unregistering the callback to stop intercepting the back gesture:
* When the user transitions to the topmost screen (activity, fragment)
* in the BackStack, unregister the callback by using
* OnBackInvokeDispatcher.unregisterOnBackInvokedCallback
* (https://developer.android.com/reference/kotlin/android/view/OnBackInvokedDispatcher#unregisteronbackinvokedcallback)
*/

}
);
}
}

Kotlin Code for Predictive Back Gesture.

@Override
fun onCreate() {
if (BuildCompat.isAtLeastT()) {
onBackInvokedDispatcher
.registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT
) {
/**
* onBackPressed logic goes here. For instance:
* Prevents closing the app to go home screen when in the
* middle of entering data to a form
* or from accidentally leaving a fragment with a WebView in it
*
* Unregistering the callback to stop intercepting the back gesture:
* When the user transitions to the topmost screen (activity, fragment)
* in the BackStack, unregister the callback by using
* OnBackInvokeDispatcher.unregisterOnBackInvokedCallback
* (https://developer.android.com/reference/kotlin/android/window/OnBackInvokedDispatcher#unregisteronbackinvokedcallback)
*/

}
}
}