Android - How to animate an activity transition when the default back button is pressed

In my activity, I have a button with the following click listener that is working great:

final ImageButton startOverButton = (ImageButton) findViewById(R.id.start_over_button);
startOverButton.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(final View v) {

        finish();//go back to the previous Activity
        overridePendingTransition(R.anim.comming_in, R.anim.comming_out);
    }
});

It animates the return to the previous activity the way I want. However, when the user presses the Android default back button, the animation is not triggered. My question is: where should I put the animation code overridePendingTransition(R.anim.comming_in, R.anim.comming_out); so that this animation will be triggered both when the user clicks on my button and in the default Android back button?

As a naive try, I have tried to put the overridePendingTransition(R.anim.comming_in, R.anim.comming_out); line of code in the onDestroy() method but it did not work.

Thank you in advance!


maybe you can do this work in onBackPressed() method in the activity.

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.comming_in, R.anim.comming_out);   
}

Basically overriding onBackPressed is a proper approach, but rather than call finish() from it i would say that is better to call super.onBackPressed() and then add overridePendingTransition so we are a bit more consistent with the inheritance rules.

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.comming_in, R.anim.comming_out);   
}

Even though overriding onBackPressed() is a good option, I would suggest overriding the finish() method, just in case the activity is finished in some other way, like a navigation action or any other view action that "destroys" the activity:

@Override public void finish() {
   super.finish();
   overridePendingTransition(0,0);
}

We need to have in consideration that this method will be triggered after the back button has been pressed, so we are good to go :-)

Update: Moreover, overriding onBackPressed() could mess up with the Activity if we are using fragments in it, because we probably don't want to be overriding the transitions every time the back is pressed.


if you use fragment you can proceed like this :

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left, R.anim.anim_slide_out_right, R.anim.anim_slide_in_right);
transaction.replace(R.id.fragment_container, new YourClassFragment);
transaction.addToBackStack(null);
transaction.commit();

anim_slide_in_left

<?xml version="1.0" encoding="utf-8"?>
 <set xmlns:android="http://schemas.android.com/apk/res/android" >
  <translate
    android:duration="500"
    android:interpolator="@android:interpolator/decelerate_quint"
    android:fromXDelta="100%p"
    android:toXDelta="0%p" >
  </translate>
 </set>

anim_slide_out_left

<?xml version="1.0" encoding="utf-8"?>
 <set xmlns:android="http://schemas.android.com/apk/res/android" >
  <translate
    android:duration="500"
    android:interpolator="@android:interpolator/decelerate_quint"
    android:fromXDelta="0%p"
    android:toXDelta="-100%p" >
  </translate>
 </set>

anim_slide_out_right

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
 <translate
    android:duration="@android:integer/config_mediumAnimTime"
    android:interpolator="@android:interpolator/decelerate_quint"
    android:fromXDelta="-100%p"
    android:toXDelta="0%p" >
 </translate>
</set>

anim_slide_in_right

<?xml version="1.0" encoding="utf-8"?>
 <set xmlns:android="http://schemas.android.com/apk/res/android" >
  <translate
    android:duration="@android:integer/config_mediumAnimTime"
    android:interpolator="@android:interpolator/decelerate_quint"
    android:fromXDelta="0%p"
    android:toXDelta="100%p" >
  </translate>
 </set>