Custom Back Button Animation

后端 未结 4 736
孤街浪徒
孤街浪徒 2020-12-14 00:13

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

相关标签:
4条回答
  • 2020-12-14 00:25

    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);
    }
    
    0 讨论(0)
  • 2020-12-14 00:30

    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);
    }
    
    0 讨论(0)
  • 2020-12-14 00:39

    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

    0 讨论(0)
  • 2020-12-14 00:44

    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)

    0 讨论(0)
提交回复
热议问题