The default animation when the Back
button is pressed is a slide from left to right. I\'d like to replace that with a custom animation. I\'m currently thinking
I think you shouldn't use finish() because the data stored by the Views will be erased
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
}
Figured it out. I wasn't finshing the current activity. The following code does the trick.
@Override
public void onBackPressed() {
[This Activity].this.finish();
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
I wouldn't use onBackPressed()
since it's a hack when we use Fragments and we need to handle the stack, for instance. I proposed a more elegant solution here:
https://stackoverflow.com/a/43725255/689723
if you want no animation
follow the code in Activity
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(0,0);
}
Reference : https://developer.android.com/reference/android/app/Activity.html#overridePendingTransition(int, int)