user246114
user246114

Reputation: 51711

Disable activity slide-in animation when launching new activity?

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.

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

Upvotes: 165

Views: 135086

Answers (13)

vovahost
vovahost

Reputation: 36069

You can also provide custom options when starting a new activity:

val intent = Intent(this, AnotherActivity.class)
startActivity(intent, ActivityOptionsCompat.makeCustomAnimation(this, 0, 0).toBundle())

Upvotes: -1

Aviroop Sanyal
Aviroop Sanyal

Reputation: 21

Let's say, we are refreshing the page with the help of button click...then use the following code snippet

        buttonRefresh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(getIntent().addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
        }
    });

Upvotes: 0

Ewoks
Ewoks

Reputation: 12445

Disclaimer: 10+ years old answer. Please refer to the latest available documentation as believe it or not things do change.

IMHO this answer 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 theme for 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

Upvotes: 198

Cactusroot
Cactusroot

Reputation: 1058

To clear things up: FLAG_ACTIVITY_NO_ANIMATION (or android:windowAnimationStyle = @null in the theme) work perfectly fine for both, enter and exit. The problem is, that the enter animation checks if the animation is enabled in the one activity and the exit animation checks it for the other one. So make sure to disable it in both activities.

Upvotes: 1

Karthika PB
Karthika PB

Reputation: 1373

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

1) created a noanim.xml in anim folder

<?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

Upvotes: 3

Alex Huiculescu
Alex Huiculescu

Reputation: 139

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

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

}

Upvotes: 3

Siddharth_Vyas
Siddharth_Vyas

Reputation: 10100

Apply

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

then

overridePendingTransition(0, 0);

This will stop the animation.

Upvotes: 44

Sasa
Sasa

Reputation: 565

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

to avoid seeing animation when you want to Intent to an Activity use:

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

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

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

Upvotes: 33

Wookie
Wookie

Reputation: 822

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.

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.

Upvotes: 10

Steve B
Steve B

Reputation: 1246

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.

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().

Upvotes: 10

cmcromance
cmcromance

Reputation: 2289

This works for me when disabling finish Activity animation.

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

Upvotes: 17

Alexander Kosenkov
Alexander Kosenkov

Reputation: 1595

Just specify Intent.FLAG_ACTIVITY_NO_ANIMATION flag when starting

Upvotes: 20

Mattias
Mattias

Reputation: 2282

The FLAG_ACTIVITY_NO_ANIMATION flag works fine for disabling the animation when starting activities.

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.

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

Upvotes: 212

Related Questions