Android 启动新活动时禁用活动滑入动画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2286315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:16:55  来源:igfitidea点击:

Disable activity slide-in animation when launching new activity?

android

提问by user246114

I have an activity which launches another activity, via a button click. By default, on newer OS versions of android, the OS will animate the new activity sliding in from right to left.

我有一个活动,通过单击按钮启动另一个活动。默认情况下,在较新的 android 操作系统版本上,操作系统将动画从右向左滑动的新活动。

Is there a way to disable this animation? I just want the new activity to appear without any sort of animation.

有没有办法禁用这个动画?我只希望新活动出现时没有任何动画。

回答by Mattias

The FLAG_ACTIVITY_NO_ANIMATIONflag works fine for disabling the animation when starting activities.

FLAG_ACTIVITY_NO_ANIMATION标志适用于在开始活动时禁用动画。

To disable the similar animation that is triggered when calling finish()on an Activity, i.e the animation slides from right to left instead, you can call overridePendingTransition(0, 0)after calling finish()and the next animation will be excluded.

要禁用调用finish()Activity时触发的类似动画,即动画从右向左滑动,您可以在调用overridePendingTransition(0, 0)后调用finish(),下一个动画将被排除。

This also works on the in-animation if you call overridePendingTransition(0, 0)after calling startActivity(...).

如果您overridePendingTransition(0, 0)在调用startActivity(...).

回答by Ewoks

IMHO thisanswer here solve issue in the most elegant way..

恕我直言,这里的答案以最优雅的方式解决问题..

Developer should create a style,

开发人员应该创建一个样式

<style name="noAnimTheme" parent="android:Theme">
  <item name="android:windowAnimationStyle">@null</item>
</style>

then in manifest set it as themefor activity or whole application.

然后在清单中将其设置为活动或整个应用程序的主题

<activity android:name=".ui.ArticlesActivity" android:theme="@style/noAnimTheme">
</activity>

Voila! Nice and easy..

瞧!好,易于..

p.s. credits to original author please..

ps感谢原作者。。

回答by Siddharth_Vyas

Apply

申请

startActivity(new Intent(FirstActivity.this,SecondActivity.class));

then

然后

overridePendingTransition(0, 0);

This will stop the animation.

这将停止动画。

回答by Sasa

In my opinion the best answer is to use "overridePendingTransition(0, 0);"

在我看来最好的答案是使用 "overridePendingTransition(0, 0);"

to avoid seeing animation when you want to Intentto an Activityuse:

以避免看到动画,当你想Intent给一个Activity使用:

this.startActivity(new Intent(v.getContext(), newactivity.class));
this.overridePendingTransition(0, 0);

and to not see the animation when you press back button Override onPausemethod in your newactivity

并且在您按下返回按钮 OverrideonPause方法时看不到动画newactivity

@Override
protected void onPause() {
    super.onPause();
    overridePendingTransition(0, 0);
}

回答by Alexander Kosenkov

Just specify Intent.FLAG_ACTIVITY_NO_ANIMATIONflag when starting

Intent.FLAG_ACTIVITY_NO_ANIMATION启动时只指定标志

回答by cmcromance

This works for me when disabling finish Activity animation.

禁用完成活动动画时,这对我有用。

@Override
protected void onPause() {
    super.onPause();
    overridePendingTransition(0, 0);
}

回答by Steve B

I'm on 4.4.2, and calling overridePendingTransition(0, 0) in the launching activity's onCreate() will disable the starting animation(calling overridePendingTransition(0, 0) immediately after startActivity() did NOT work). As noted in another answer, calling overridePendingTransition(0, 0) after finish() disables the closing animation.

我在 4.4.2 上,在启动活动的 onCreate() 中调用 overridePendingTransition(0, 0)将禁用启动动画(在 startActivity() 不起作用后立即调用 overridePendingTransition(0, 0) )。如另一个答案所述,在 finish() 之后调用 overridePendingTransition(0, 0) 会禁用关闭动画。

Btw, I found that setting the style with "android:windowAnimationStyle">@null (another answer mentioned here) caused a crash when my launching activity tried to set the action bar title. Debugging further, I discovered that somehow this causes window.hasFeature(Window.FEATURE_ACTION_BAR) to fail in the Activity's initActionBar().

顺便说一句,当我的启动活动尝试设置操作栏标题时,我发现使用“android:windowAnimationStyle">@null(此处提到的另一个答案)设置样式会导致崩溃。进一步调试,我发现这会导致 window.hasFeature(Window.FEATURE_ACTION_BAR) 在 Activity 的 initActionBar() 中失败。

回答by Wookie

FLAG_ACTIVITY_NO_ANIMATION may work, but wasn't doing the trick for me when combined with FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK. I'm apparently seeing the animation for creating a new task with a fresh activity stack as I navigate laterally to my other top-level views.

FLAG_ACTIVITY_NO_ANIMATION 可能会起作用,但是当与 FLAG_ACTIVITY_CLEAR_TASK 和 FLAG_ACTIVITY_NEW_TASK 结合使用时,它对我不起作用。当我横向导航到我的其他顶级视图时,我显然看到了使用新的活动堆栈创建新任务的动画。

What did work here was calling "overridePendingTransition(0, 0);" either immediately after my startActivity() call or the onPause(). Both ways worked, but doing it after startActivity() gives me a little more control over when I want animations and when I don't.

在这里起作用的是调用“overridePendingTransition(0, 0);” 在我的 startActivity() 调用或 onPause() 之后立即。两种方法都有效,但在 startActivity() 之后执行它可以让我更好地控制何时需要动画以及何时不需要。

回答by Karthika PB

I had a similar problem of getting a black screen appear on sliding transition from one activity to another using overridependingtransition. and I followed the way below and it worked

我有一个类似的问题,即使用overridependingtransition. 我按照下面的方法行事

1) created a noanim.xmlin anim folder

1)noanim.xml在anim文件夹中创建一个

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

and used

并使用

overridePendingTransition(R.drawable.lefttorightanim, R.anim.noanim);

The first parameter as my original animation and second parameter which is the exit animation as my dummy animation

第一个参数是我的原始动画,第二个参数是退出动画作为我的虚拟动画

回答by Alex Huiculescu

In order to avoid the black background when starting an activity already in the stack, I added overridePendingTransition(0,0)in onStart():

为了避免在启动堆栈中已有的活动时出现黑色背景,我添加 overridePendingTransition(0,0)onStart()

@Override
protected void onStart() {
    overridePendingTransition(0,0);
    super.onStart();

}