24 maggio 2024
Before you read on, this topic is scoped to Views. Predictive Back with Compose is easier to implement and not included in this blog post. To learn how to implement Compose with Predictive Back, see the Add predictive back animations codelab and the I/O workshop Improve the user experience of your Android app.
This blog post aims to shed light on various dependencies and requirements to support predictive back animations in your views based app.
First, view the Predictive Back Requirements table to understand if a particular animation requires a manifest flag, a compileSDK version, additional libraries or hidden developer options to function.
Then, start your quest. Here are your milestones:
The first milestone is to upgrade to Kotlin 1.8.0 or higher, which is required for other Predictive Back dependencies.
The back-to-home animation is the keystone predictive back animation.
To get this animation, add android:enableOnBackInvokedCallback=true
in your AndroidManifest.xml
for your root activity if you are a multi-activity app (see per-activity opt-in) or at the application level if you are a single-activity app. After this, you’ll see both the back-to-home animation and a cross-task animation where applicable, which are visible to users in Android 15+ and behind a developer option in Android 13 and 14.
If you are intercepting back events in your root activity (e.g. MainActivity), you can continue to do so but you’ll need to use supported APIs and you won’t get the back-to-home animation. For this reason, we generally recommend you only intercept back events for UI logic; for example, to show a dialog asking the user to save before they quit.
See the Add support for the predictive back gesture guide for more details.
If you are a multi-activity app, you’ll need to opt-in and handle back events within those activities too to get a system controlled cross-activity animation. Learn more about per-activity opt-in, available for devices running Android 14+. The cross-activity animation is visible to users in Android 15+ and behind a developer option in Android 13 and 14.
Custom cross activity animations are also available with overrideActivityTransition.
Next, you’ll want to focus on your fragment animations and transitions. This requires updating to AndroidX fragment 1.7.0 and transition 1.5.0 or later and using Animator or AndroidX Transitions. Assuming these requirements are met, your existing fragment animations and transitions will animate in step with the back gesture. You can also use material motion with fragments. Most material motions support predictive back as of 1.12.02-alpha02 or higher, including MaterialFadeThrough, MaterialSharedAxis and MaterialFade.
Don’t strive to make your fragment transitions look like the system’s cross-activity transition. We recommend this full screen surface transition instead.
Learn more about Fragments and Predictive Back.
Finally, you’ll want to take advantage of the Material Component View animations available for Predictive Back. Learn more about available components.
After this, you’ve completed your quest to support Predictive Back animations in your view based app.
If you’re up for more, you might also ensure your AndroidX transitions are supported with Predictive Back. Read more about AndroidX Transitions and the Predictive Back Progress APIs.
[Optional] AndroidX Transitions milestone
Other Resources