Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Back Button Animation

Tags:

android

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 that some combination of onBackPressed() and overridePendingTransition will do the trick, but I haven't been able to get it working.

like image 714
dfetter88 Avatar asked Jul 20 '10 19:07

dfetter88


3 Answers

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);
}
like image 138
Álvaro Avatar answered Oct 03 '22 12:10

Álvaro


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);
}
like image 23
dfetter88 Avatar answered Oct 03 '22 12:10

dfetter88


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)

like image 20
최봉재 Avatar answered Oct 03 '22 12:10

최봉재