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
Custom Back Button Animation
提问by dfetter88
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.
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 并且我们需要处理堆栈时,这是一个黑客。我在这里提出了一个更优雅的解决方案: