How can I add an animation to the activity finish()
Asked Answered
D

8

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?

Depart answered 2/12, 2010 at 0:49 Comment(0)
I
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 :)

Improvvisatore answered 5/1, 2012 at 11:40 Comment(6)
by "Details", @Improvvisatore 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.xAphid
How does R.anim.nothing look like?Schulte
Use @Felipe Micaroni Lalli answer, 0 mean no animation.Rolon
This like helped me too. It explains how to define your own animations: #5152091Holst
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); }Epictetus
The above transition is executed only if enabled in Developer options, see https://mcmap.net/q/127621/-activity-transition-in-android.Ovotestis
T
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);
}
Tranche answered 5/8, 2015 at 13:9 Comment(1)
This could be the accepted answerScottscotti
R
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);
    }

}
Radiothermy answered 20/5, 2015 at 10:56 Comment(1)
Why is that better?Mannerism
A
23
finish();
overridePendingTransition(0, 0);
Ard answered 13/3, 2013 at 22:59 Comment(0)
Z
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);
}
Zither answered 10/6, 2014 at 12:35 Comment(0)
W
12

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

Weinrich answered 2/12, 2010 at 0:55 Comment(3)
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!Depart
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.Isthmus
@Zapnologica this other answer I gave for a similar question spells it out a bit more. #4941074Weinrich
A
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>

Aphid answered 26/10, 2012 at 0:13 Comment(0)
P
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);
}
Photojournalism answered 25/9, 2013 at 11:31 Comment(2)
This works on back press (physical button). To make it also work with the "up" arrow, see this answer: https://mcmap.net/q/205396/-onactivityresult-is-not-called-when-the-back-button-in-actionbar-is-clicked/20161352#20161352Secor
Important to note: overridePendingTransition needs to be called before calling super.onActivityResult. Otherwise the pendint transition will not be overridden!Meyers

© 2022 - 2024 — McMap. All rights reserved.