hotwired / turbo-android

Android framework for making Turbo native apps
MIT License
424 stars 50 forks source link

Support Android 13's predictive back gesture #276

Closed jayohms closed 1 year ago

jayohms commented 1 year ago

This allows the library's navigation to respect Android 13/14's new predictive back gesture. Documentation: https://developer.android.com/guide/navigation/predictive-back-gesture

This disable the Activity's OnBackPressedCallback when the current nav controller is at its start destination. This allows the system to take control over the back behavior when leaving the app and display a custom animation.

Even if a particular user doesn't not have the "Preditive back animations" Developer Option enabled in Android 13, this change still provides a tangible benefit. The app's main Activity will no longer .finish() when using the back gesture/button to leave the app. For the first time, Android 13 maintains the Activity instance if an app does not manually call .finish().

On Android 13, apps that want to opt into the predictive back gesture must enable the following AndroidManifest flag:

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

Note that Android 14 will further expand on the predictive back gesture including between screens, but the AndroidX APIs are not finalized and it requires targeting SDK 34, which hasn't hit API stability yet.