106

I'm using overridePendingTransition for when my activity is created and that works fine I can see the fade in works great, but when I try and animate the finish on the activity it is still doing the default right to left slide.

I first tried defining the out animation when I start the activity as follows:

Intent myIntent = new Intent(a, SkdyAlert.class);
    myIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    a.startActivity(myIntent);
    if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT) {
        AnimationHelper.overridePendingTransition(a, R.anim.fadein, R.anim.fadeout);
    }

Then I tried doing it when I finish the activity as well

okBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            finish();
            if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT) {
                AnimationHelper.overridePendingTransition(activity, 0, R.anim.fadeout);
            }
        }
    });

But neither of these approaches will prevent the "right to left" slide for the exit animation. Any ideas on what I'm doing wrong?

b-ryce
  • 5,752
  • 7
  • 49
  • 79

8 Answers8

243

I override pending transition just after calling finish();

In my case, I have done it to avoid transitions.

finish();
Details.this.overridePendingTransition(R.anim.nothing,R.anim.nothing);

Order is important :)

John Doe
  • 397
  • 3
  • 18
Goofyahead
  • 5,874
  • 6
  • 29
  • 33
  • 6
    by "Details", @Goofyahead is naming his enclosing Activity--ymmv. This technique worked for me to replace an animation from Theme.Dialog on 2.x, but not 3.x or 4.x – larham1 Oct 25 '12 at 23:49
  • 10
    How does `R.anim.nothing` look like? – sandalone Aug 24 '13 at 14:23
  • Use @Felipe Micaroni Lalli answer, 0 mean no animation. – Yeung Jan 24 '14 at 05:23
  • This like helped me too. It explains how to define your own animations: https://stackoverflow.com/questions/5151591/android-left-to-right-slide-animation – Adam Apr 03 '14 at 00:50
  • 27
    You can override `finish()` method to avoid transitions in all cases (back button pressed, as sample): `@Override public void finish() { super.finish(); overridePendingTransition(0, 0); }` – eugeneek Dec 01 '15 at 09:47
  • The above transition is executed only if enabled in Developer options, see http://stackoverflow.com/a/30422015/2914140. – CoolMind Jul 25 '16 at 11:00
42

This question has already answered but the most efficient way to put an animation while exiting from an activity is by overriding the "finish()" method of the related activity:

@Override
public void finish() {
    super.finish();
    overridePendingTransition(R.anim.hold, R.anim.slide_out_bottom);
}
Bahadir Tasdemir
  • 10,325
  • 4
  • 49
  • 61
26

I would suggest to use isFinishing() method to configure the animations at onPause instead of calling finish()

@Override
protected void onPause() {
    super.onPause();
    if (isFinishing()){
        overridePendingTransition(R.anim.activity_slide_in, R.anim.activity_slide_out);
    }

}
Pablo A. Martínez
  • 2,547
  • 2
  • 23
  • 28
23
finish();
overridePendingTransition(0, 0);
Felipe
  • 16,649
  • 11
  • 68
  • 92
20

I fixed this issue using this kind of approach:

to open with animation:

 Intent newUser = new Intent(getBaseContext(), NewUserActivity.class);
    startActivity(newUser);
    overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);

To close with animation:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    onBackPressed();
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.slide_out_right,R.anim.slide_in_right);
}
Deivison Sporteman
  • 2,334
  • 1
  • 19
  • 22
12

Look into doing it through a theme. You can define enter exit animations for activities or the entire application

Nathan Schwermann
  • 31,285
  • 16
  • 80
  • 91
  • YES! That was it. Well kinda. I had already applied a theme, and the theme had an animation defined, so thats why I couldn't get my own animation to work. Thanks a ton for the insight! – b-ryce Dec 02 '10 at 01:11
  • 3
    can you give me a hint where to look which transistions will be delivered by the themes. I also would like to set a custom transition to all sites but i don't know how. – 0xPixelfrost Dec 27 '11 at 10:42
  • @Zapnologica this other answer I gave for a similar question spells it out a bit more. http://stackoverflow.com/questions/4940574/how-to-override-the-enter-activity-animation-if-it-is-stated-by-launcher/4940610#4940610 – Nathan Schwermann Oct 07 '13 at 15:24
6

Following on the answer by @schwiz, here is an animation override for the built-in Dialog theme, where I have defined local slide_up and slide_down animations. My activity specifies the theme MyDialog in order to have these transitions in and out.

<style name="Animation.MyDialog" parent="android:Animation.Dialog">
    <item name="android:windowEnterAnimation">@anim/slide_up</item>
    <item name="android:windowExitAnimation">@anim/slide_down</item>
</style>

<style name="Theme.MyDialog" parent="android:Theme.Dialog">
    <item name="android:windowAnimationStyle">@style/Animation.MyDialog</item>
</style>

larham1
  • 11,736
  • 5
  • 35
  • 26
6

Use startActivityForResult to start your child activity and in onActivityResult() of your parent activity:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==REQUEST_YOUR_ACTIVITY) {
        overridePendingTransition(R.anim.parent_appearing_anim, R.anim.child_dissmissing_anim);
    }
    super.onActivityResult(requestCode, resultCode, arg2);
}
Chris.Zou
  • 4,506
  • 6
  • 31
  • 38
  • This works on back press (physical button). To make it also work with the "up" arrow, see this answer: [https://stackoverflow.com/questions/20161234/onactivityresult-is-not-called-when-the-back-button-in-actionbar-is-clicked/20161352#20161352](https://stackoverflow.com/questions/20161234/onactivityresult-is-not-called-when-the-back-button-in-actionbar-is-clicked/20161352#20161352) – lenooh Jan 11 '16 at 16:13
  • Important to note: overridePendingTransition needs to be called before calling super.onActivityResult. Otherwise the pendint transition will not be overridden! – goemic Apr 15 '20 at 15:26