overridePendingTransition和FLAG_ACTIVITY_REORDER_TO_FRONT同时使用时,没有Activity启动动画

转自: http://stackoverflow.com/questions/4633543/overridependingtransition-does-not-work-when-flag-activity-reorder-to-front-is-u


I have two activities in the stack, in order to show them I use FLAG_ACTIVITY_REORDER_TO_FRONT. So far so good, the problem comes when I want to bring the activity with an animation using overridePendingTransition.

Intent i = new Intent(ActivityA.this, ActivityB.class);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
ActivityA.this.startActivity(i);
overridePendingTransition(R.anim.transition_to_right, R.anim.transition_to_left);

The transition is not shown, however, if the flag is not added to the intent (removing line 2) then there is no problem.

Is it possible to bring an activity to front with an animation?


*******************解决方案一******************

I ran into this same problem. The trick is to override the transition in the onResume() callback. 

@Override
public void onResume() {
    super.onResume();
    overridePendingTransition(R.anim.transition_to_right, R.anim.transition_to_left);
}

*******************解决方案二 ******************

Actually the right solution when using REORDER_TO_FRONT is to call overridePendingTransition in the method onNewIntent() of the activity you are going to open.

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}

replace with your transitions.

If you need to selectively replace the transition you can add an extra in your intent and check it in the onNewIntent() to decide what to do.

This is not suitable if you don't know the transition that will be opened (if you don't specify it explicitly for example) but otherwise is the right solution.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android开发中,页面切换动画可以通过设置Activity切换动画来实现。可以在res目录下的anim文件夹中定义一些动画效果,然后在代码中使用动画即可。 以下是一些常见的动画效果: 1. 淡入淡出效果 在anim文件夹下新建fade_in.xml和fade_out.xml文件,分别设置动画效果: fade_in.xml: ``` <?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fromAlpha="0.0" android:interpolator="@android:anim/accelerate_interpolator" android:toAlpha="1.0" /> ``` fade_out.xml: ``` <?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fromAlpha="1.0" android:interpolator="@android:anim/accelerate_interpolator" android:toAlpha="0.0" /> ``` 然后在Activity使用如下代码即可: ``` startActivity(intent); overridePendingTransition(R.anim.fade_in, R.anim.fade_out); ``` 2. 旋转效果 在anim文件夹下新建rotate.xml文件,设置动画效果: ``` <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fromDegrees="0" android:interpolator="@android:anim/accelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360" /> ``` 然后在Activity使用如下代码即可: ``` startActivity(intent); overridePendingTransition(R.anim.rotate, R.anim.rotate); ``` 3. 缩放效果 在anim文件夹下新建scale.xml文件,设置动画效果: ``` <?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fromXScale="0.0" android:fromYScale="0.0" android:interpolator="@android:anim/accelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.0" android:toYScale="1.0" /> ``` 然后在Activity使用如下代码即可: ``` startActivity(intent); overridePendingTransition(R.anim.scale, R.anim.scale); ``` 以上是一些常见的Android页面切换动画效果,你可以根据自己的需要进行修改和定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值