Use Resource Animations for Android Activity Transitions
Android activity-transition methods accept animation resource IDs. An Animation instance created in code is a different object and cannot be substituted for an integer resource identifier. Define the window-transition animation in res/anim, then pass its generated ID to the activity API.
For Android 14, API level 34, and later, overrideActivityTransition is the current framework method. The older overridePendingTransition method was deprecated at that API level. This example keeps the system's normal behavior on older devices rather than adding a separate legacy override.
Activity API
Build the application with compileSdk 34 or later. The runtime version guard supports older devices, but an older compile SDK cannot resolve this API.
Define two animation resources
In an Android Kotlin project, create app/src/main/res/anim/detail_enter.xml:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="180" />
Create app/src/main/res/anim/detail_exit.xml:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="180" />
These are view-animation resources addressed as R.anim.detail_enter and R.anim.detail_exit. Property animators under res/animator are a different resource family. Keeping the files in the correct directory lets Android generate the expected identifiers.
Animation resource formats
Configure the destination Activity
Add this class in the application's own package, adjusting the package declaration to match the project. The example uses a plain framework Activity and programmatic content, so it does not require an XML layout or an AppCompat theme.
package com.example.transitiondemo
import android.app.Activity
import android.os.Build
import android.os.Bundle
import android.widget.Button
class DetailActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (Build.VERSION.SDK_INT >= 34) {
overrideActivityTransition(
OVERRIDE_TRANSITION_OPEN,
R.anim.detail_enter,
R.anim.detail_exit
)
overrideActivityTransition(
OVERRIDE_TRANSITION_CLOSE,
R.anim.detail_enter,
R.anim.detail_exit
)
}
val close = Button(this).apply {
text = "Return"
setOnClickListener { finish() }
}
setContentView(close)
}
}
Declare the destination inside the existing manifest's application element:
<activity
android:name=".DetailActivity"
android:exported="false" />
In an existing source Activity, launch it from a user action with an explicit intent:
startActivity(android.content.Intent(this, DetailActivity::class.java))
This is an internal destination, so it needs no external intent filter. Keep the normal same-task launch behavior for the example. Both the opening and closing override are configured on the destination that is on top during the relevant transition. Explicit intents
Verify navigation rather than only the animation
On API 34 or later, the destination should use the short fade when opened and when Return finishes it. On an older supported device, the guarded code is skipped and the system transition remains. Test with normal animation settings before deciding that an animation resource is ineffective.
Also test a system Back action, rotation and restoration after the Activity is recreated. A visual effect must not change which screen owns the navigation state or leave an invisible Activity on the stack. Keep the custom transition subtle enough that removing animations does not remove essential feedback.
Predictive back can let the system control the gesture's preview phase. A custom committed transition is not permission to replace or intercept the back gesture. Follow the platform's current back-navigation guidance for the application's navigation stack. Predictive back guidance
Do not combine this example with a shared-element scene-transition bundle and expect both effects to run. The framework documents precedence and limitations, including same-task behavior. For an object whose properties need to animate inside one screen, use the relevant view/property animation API instead of trying to pass that object into a window-transition method.
This is a newly authored answer to Herman Schoenfeld's resource-ID question , licensed CC BY-SA 3.0 . The original post is a question, not an owner-authored solution. The Kotlin and resource examples are documentation-checked and still require an Android build and device or emulator run.
Author: Herman Schoenfeld