You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 13中overridePendingTransition退出动画失效问题求助

Android 13下FLAG_ACTIVITY_REORDER_TO_FRONT导致退出动画失效的解决办法

问题描述

设备升级至Android 13后,调用overridePendingTransition时仅进入动画(enterAnim)生效,退出动画(exitAnim)失效。应用流程为:Activity A → Activity B → 通过FLAG_ACTIVITY_REORDER_TO_FRONT回到Activity A,该场景下Activity B的退出动画在Android 13以外的系统版本均正常工作。

涉及代码

Activity B 启动逻辑

Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
overridePendingTransition(0, R.anim.push_bottom_out);

Activity A onNewIntent 处理

@Override
protected void onNewIntent(Intent intent) {
  super.onNewIntent(intent);
  overridePendingTransition(0, R.anim.push_bottom_out);
}

动画资源 push_bottom_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="0%p"
        android:toYDelta="80%p"
        android:duration="300"/>

    <alpha
        android:fromAlpha="1"
        android:toAlpha="0.3"
        android:duration="300" />
</set>

解决方案

Android 13对Activity转场动画的调度逻辑做了变更,尤其是使用FLAG_ACTIVITY_REORDER_TO_FRONT时,原有的overridePendingTransition调用方式不再可靠,可通过以下步骤修复:

  1. 移除冗余的动画调用
    删除Activity A中onNewIntent方法内的overridePendingTransition调用,该调用会干扰Activity B的退出动画执行逻辑。

  2. 改用ActivityOptions指定动画
    在Activity B中启动Activity A时,使用ActivityOptions.makeCustomAnimation显式绑定转场动画,替代直接调用overridePendingTransition:

    Intent intent = new Intent(this, A.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    // 参数说明:第一个为目标Activity的进入动画,第二个为当前Activity的退出动画
    ActivityOptions options = ActivityOptions.makeCustomAnimation(this, 0, R.anim.push_bottom_out);
    startActivity(intent, options.toBundle());
    
  3. 兼容检查(可选)
    若动画仍不生效,可将push_bottom_out.xml复制到res/anim-v33目录下,确保Android 13系统能正确加载对应版本的动画资源。

内容的提问来源于stack exchange,提问作者jerry lam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 16:09:25