Android - How to stop animation between activity changes
Asked Answered
N

5

26

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.

Northwesterly answered 14/4, 2011 at 23:11 Comment(0)
W
43

Have you tried overridePendingTransition()?

Wail answered 15/4, 2011 at 0:26 Comment(3)
No I hadn't tried 'overridePendingTransition(0,0)'. I used that in each Activities onResume() and onPause(). It worked perfect. Thanks!!!Northwesterly
It helped me on my Nexus 7 but it did not on my SGS4. Is there any other way to fix this ? Thanks.Should
@oleg.semen: Try the theme approach from another answer on this question.Wail
L
18

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

Lonee answered 24/9, 2013 at 4:32 Comment(0)
O
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.

Oberstone answered 18/6, 2013 at 9:27 Comment(2)
But isnt this wrong "making no animation" fix, while allowing the activitie still being created?Sangfroid
I believe bringing the activity from the back stack without animation, might be the best possible solution around.Oberstone
P
3

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

Performative answered 14/11, 2016 at 14:3 Comment(0)
W
1

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.

Westbrooks answered 21/3, 2017 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.