Android 自定义后退按钮动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3293765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:32:32  来源:igfitidea点击:

Custom Back Button Animation

android

提问by dfetter88

The default animation when the Backbutton 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 overridePendingTransitionwill do the trick, but I haven't been able to get it working.

Back按下按钮时的默认动画是从左向右滑动。我想用自定义动画替换它。我目前认为的某种组合onBackPressed(),并overridePendingTransition会做的伎俩,但我一直没能得到它的工作。

回答by álvaro

I think you shouldn't use finish() because the data stored by the Views will be erased

我认为你不应该使用 finish() 因为视图存储的数据将被删除

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

回答by dfetter88

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

回答by bong jae choe

if you want no animation

如果你不想动画

follow the code in Activity

按照代码 Activity

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

Reference : https://developer.android.com/reference/android/app/Activity.html#overridePendingTransition(int, int)

参考:https: //developer.android.com/reference/android/app/Activity.html#overridePendingTransition(int , int)

回答by cesards

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:

我不会使用,onBackPressed()因为当我们使用 Fragments 并且我们需要处理堆栈时,这是一个黑客。我在这里提出了一个更优雅的解决方案:

https://stackoverflow.com/a/43725255/689723

https://stackoverflow.com/a/43725255/689723