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

AndroidAnimationAndroid Activity

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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);
}

Solution 3 - Android

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);
    }

}

Solution 4 - Android

finish();
overridePendingTransition(0, 0);

Solution 5 - Android

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);
}

Solution 6 - Android

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

Solution 7 - Android

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>

Solution 8 - Android

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);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionb-ryceView Question on Stackoverflow
Solution 1 - AndroidGoofyaheadView Answer on Stackoverflow
Solution 2 - AndroidBahadir TasdemirView Answer on Stackoverflow
Solution 3 - AndroidPablo A. MartínezView Answer on Stackoverflow
Solution 4 - AndroidFelipeView Answer on Stackoverflow
Solution 5 - AndroidDeivison SportemanView Answer on Stackoverflow
Solution 6 - AndroidNathan SchwermannView Answer on Stackoverflow
Solution 7 - Androidlarham1View Answer on Stackoverflow
Solution 8 - AndroidChris.ZouView Answer on Stackoverflow