Android - How to stop animation between activity changes

。_饼干妹妹 提交于 2019-11-27 15:38:57

问题


I have multiple different Activity in my app and I don't want any transition animation when changing between Activities. Below is the how I'm changing between Activities:

Intent i = new Intent(FirstActivity.this, SecondActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(i);

This works great the first time I start a new Activity. There is no animation, but when I go back to an Activity that is already started it seems like the "Intent.FLAG_ACTIVITY_NO_ANIMATION" is ignored and the default animation happens.

I can't seem to figure out why this is happening.


回答1:


Have you tried overridePendingTransition()?




回答2:


You can set FLAG_ACTIVITY_REORDER_TO_FRONT by code and FLAG_ACTIVITY_NO_ANIMATION in manifest as below:

Create noAnimTheme in res/values/styles.xml

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

or

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

and use it in manifest:

<activity android:name="SecondActivity" android:theme="@style/noAnimTheme"/>

I hope it helps




回答3:


I wa needing this as I had to create activities on clicking the menus.

I did the following :

I added the FLAG_ACTIVITY_NO_ANIMATION flag to the intent. It stopped the animations while creating the activity for the first time.

However the activities in the stack which were called when we click on the same menu again (probably from a different activity), it had the animation.

So I added FLAG_ACTIVITY_NO_HISTORY to clear or rather finish the activity when it starts a new activity. This caused to create a new activity (without animation) when I click on the menu once again.




回答4:


add this after creating the second intent

        Intent i = new Intent(SecondActivity.this, FirstActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(i);

when you return to the first intent, animation is disabled, worked for me though




回答5:


If you're using FLAG_ACTIVITY_REORDER_TO_FRONT then you can also override onNewIntent for later startActivity calls. This will just work for bring to front states instead of first call.

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

Sure, you must implement this in target activity.



来源:https://stackoverflow.com/questions/5670754/android-how-to-stop-animation-between-activity-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!