Android - How to stop animation between activity c

2019-01-23 12:23发布

问题:

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.