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

b-ryce picture b-ryce · Dec 2, 2010 · Viewed 95.8k times · Source

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?

Answer

Goofyahead picture Goofyahead · Jan 5, 2012

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 :)