> 文章列表 > 禁用Android切换动画

禁用Android切换动画

禁用Android切换动画

禁用Android切换动画

前言

最近有个功能要禁用安卓activity的切换动画,找了几个方法,这里记录下。

使用Theme

最简单的就是设置没有动画的主题了,在activity上增加notAnimation的theme属性。

android:theme="@style/Theme.notAnimation"

但是这里把整个activity的动画都禁了,如果有时候需要动画有时候不需要就没法用了。

使用FLAG_ACTIVITY_NO_ANIMATION

Intent intent = new Intent("action");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);

这里使用了Intent的flag,但是我发现好像没什么效果,有的说是不能finish,我这也没finish这个页面啊。。

使用overridePendingTransition(0, 0)

Intent intent = new Intent("action");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
overridePendingTransition(0, 0)

后面又看到说在startActivity后增加overridePendingTransition的调用,但是我这也是不生效的。

后面在stack overflow上看到说放pause里或者onResume生效,这里我就放onResume里了,是生效的。

    @Overrideprotected void onNewIntent(Intent intent) {super.onNewIntent(intent);flag = true;}boolean flag = false;@Overrideprotected void onResume() {super.onResume();// 禁用动画if (flag) {overridePendingTransition(0, 0);onEvaluateBack = false;}}

这里代码简化了下,大致就是通过onCreate或onNewIntent来判断是否要禁用动画,如果需要禁用动画,就在onResume里禁用了。

99银饰网