Android中如何切换没有动画的Activity?

发布于 2024-11-28 05:05:39 字数 781 浏览 1 评论 0原文

如何在AndroidManifest文件中正确使用Intent标志FLAG_ACTIVITY_NO_ANIMATION?我认为我的问题很微不足道,但我找不到好的例子或解决方案。

    <intent-filter>
        <data android:name="android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION" />
    </intent-filter>

然而,编译器没有报告错误,但 data 不正确。 我只想在活动之间切换时禁用动画。我可以在 onCreate 或 onResume 中使用 getWindow().setWindowAnimations(0); 但使用 flag 是更好的方法,不是吗?

我也可以在代码中使用:

    Intent intent = new Intent(v.getContext(), newactivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    getContext().startActivity(intent);

但我想在 Android Manifest 中使用这个标志。在从第二个活动返回到第一个活动的情况下也禁用动画。

How can I use properly the Intent flag FLAG_ACTIVITY_NO_ANIMATION in AndroidManifest file? I supose my problem is trivial, but I can't find good example or solution to it.

    <intent-filter>
        <data android:name="android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION" />
    </intent-filter>

However no error is reported by compliator, but data isn't correct.
I just want to disable animation in case switching between activities. I can use getWindow().setWindowAnimations(0); in onCreate or onResume rather but using flag is better way, isn't it?

I can use also in code:

    Intent intent = new Intent(v.getContext(), newactivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    getContext().startActivity(intent);

But I want to use this flag in Android Manifest. To disable animation also in case returning from second activity to first.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

皇甫轩 2024-12-05 05:05:39

您可以创建样式,

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

并将其设置为清单中活动的主题:

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

您还可以定义样式来指定自定义进入和退出动画。
http://developer.android.com/reference/android/R.attr.html#windowEnterAnimation< /a>

You can create a style,

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

and set it as theme for your activity in the manifest:

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

You can also define a style to specify custom entry and exit animations.
http://developer.android.com/reference/android/R.attr.html#windowEnterAnimation

温暖的光 2024-12-05 05:05:39

如果您的上下文是一个 Activity,您可以调用 overridePendingTransition

在 startActivity(Intent) 或其中一种风格之后立即调用
finish 指定接下来要执行的显式过渡动画。

因此,以编程方式:

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

If your context is an activity you can call overridePendingTransition:

Call immediately after one of the flavors of startActivity(Intent) or
finish to specify an explicit transition animation to perform next.

So, programmatically:

this.startActivity(new Intent(v.getContext(), newactivity.class));
this.overridePendingTransition(0, 0);
能怎样 2024-12-05 05:05:39

试试这个代码,

this.startActivity(new Intent(v.getContext(), newactivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));

Try this code,

this.startActivity(new Intent(v.getContext(), newactivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
傲娇萝莉攻 2024-12-05 05:05:39

您也可以在您不想从中转换的所有活动中执行此操作:

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

我喜欢这种方法,因为您不必打乱活动的风格。

You can also just do this in all the activities that you dont want to transition from:

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

I like this approach because you do not have to mess with the style of your activity.

酸甜透明夹心 2024-12-05 05:05:39

主题风格中的线条效果很好,但用白屏代替了动画。尤其是在速度较慢的手机上 - 这真的很烦人。
因此,如果您想要即时过渡 - 您可以在主题样式中使用它:

<item name="android:windowAnimationStyle">@null</item>
<item name="android:windowDisablePreview">true</item>

The line in the theme style works fine, yet that replaces the animation with a white screen. Especially on a slower phone - it is really annoying.
So, if you want an instant transition - you could use this in the theme style:

<item name="android:windowAnimationStyle">@null</item>
<item name="android:windowDisablePreview">true</item>
笙痞 2024-12-05 05:05:39

这是一个单行解决方案,适用于低至 minSdkVersion 14 的版本,您应该将其插入 res/styles.xml 中:

<item name="android:windowAnimationStyle">@null</item>

如下所示:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        ...
        <item name="android:windowAnimationStyle">@null</item>
    </style>
    ...
</resources>

干杯!

Here is a one-liner solution that works for as low as minSdkVersion 14 which you should insert in you res/styles.xml:

<item name="android:windowAnimationStyle">@null</item>

like so:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        ...
        <item name="android:windowAnimationStyle">@null</item>
    </style>
    ...
</resources>

Cheers!

℡寂寞咖啡 2024-12-05 05:05:39

启动意图后,您可以使用此代码:

Intent intent = new Intent(Activity1.this, Activity2.class);
overridePendingTransition(0, 0);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);

如果使用,意图将在没有动画或过渡的情况下工作

After starting intent you can use this code :

Intent intent = new Intent(Activity1.this, Activity2.class);
overridePendingTransition(0, 0);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);

If used, intent will work with no animations or transitions

淡淡の花香 2024-12-05 05:05:39

这不是示例使用或解释如何使用 FLAG_ACTIVITY_NO_ANIMATION,但它确实回答了如何禁用 Activity 切换动画,如问题标题中所述:

Android,如何禁用开始新活动时有“擦除”效果吗?

This is not an example use or an explanation of how to use FLAG_ACTIVITY_NO_ANIMATION, however it does answer how to disable the Activity switching animation, as asked in the question title:

Android, how to disable the 'wipe' effect when starting a new activity?

(り薆情海 2024-12-05 05:05:39

创建您自己的样式覆盖 android:Theme

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

然后在清单中使用它,如下所示:

<activity android:name=".MainActivity"
    android:theme="@style/noAnimationStyle">
</activity>

create your own style overriding android:Theme

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

Then use it in manifest like this:

<activity android:name=".MainActivity"
    android:theme="@style/noAnimationStyle">
</activity>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文