不要在 onBackPressed 上调用 onDestroy
我不希望当按下“后退”按钮时我的活动被破坏。我的应用程序与 1.6 SDK 兼容。参考 http:// android-developers.blogspot.com/2009/12/back-and-other-hard-keys- Three-stories.html 和 覆盖后退按钮以充当主页按钮,我选择了以下代码:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// For versions lower than 2.0
if (Utility.buildDet.getDeviceBuildAPI() <= Utility.buildDet.getBuildApi()
&& keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
onBackPressed();
return super.onKeyDown(keyCode, event);
}
// In any version, this function will be called
public void onBackPressed() {
// This will be called either automatically for you on 2.0
// or later, or by the code above on earlier versions of the platform.
Log.i(TAG, "##### BACK KEY IS PRESSED");
this.moveTaskToBack(true); // on false, it shows moveTaskToBack: 11
return;
}
这些日志
: ##### BACK KEY IS PRESSED
INFO/ActivityManager(51): moveTaskToBack: 10
: !!!!!!! Into onPause
: !!!!!!! Into onStop
: !!!!!!! Into DESTROY
当我按后退按钮时,我没有覆盖 moveTasToBack()。 Anu clu 我该怎么做才能在按下后退按钮时不被破坏。也许我想忽略按钮或隐藏活动。
任何线索,为什么它没有按预期工作。
谢谢
I don't want my acivity to get destroyed when Back button is pressed. My app is compatible from 1.6 SDK's. Referring to http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html and Override back button to act like home button, I opted the following code :
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// For versions lower than 2.0
if (Utility.buildDet.getDeviceBuildAPI() <= Utility.buildDet.getBuildApi()
&& keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
onBackPressed();
return super.onKeyDown(keyCode, event);
}
// In any version, this function will be called
public void onBackPressed() {
// This will be called either automatically for you on 2.0
// or later, or by the code above on earlier versions of the platform.
Log.i(TAG, "##### BACK KEY IS PRESSED");
this.moveTaskToBack(true); // on false, it shows moveTaskToBack: 11
return;
}
When I press Back button, I these logs
: ##### BACK KEY IS PRESSED
INFO/ActivityManager(51): moveTaskToBack: 10
: !!!!!!! Into onPause
: !!!!!!! Into onStop
: !!!!!!! Into DESTROY
I have not overridden moveTasToBack(). Anu clu what do I do to not get destroyed when back button is pressed. Maybe I want to just ignore the button or hide the activity.
Any clue, why it is not working as expected.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么要改变标准系统行为? Android 系统可以破坏 Activity,也可以不破坏 Activity。它应该仅取决于系统。您可能想做一些可以以不同方式完成的事情。也许您需要服务或实施保存活动状态?
Why do you want to change standard system bahaviour? Android system can destroy activity or not. It should depend only on the system. You probably want to do something that can be done differently. Maybe you need services or implement saving state of activity?
捕获返回键并不能解决这个问题。 Android 中的 Activity 生命周期最好留给系统。系统会随时终止您的活动。
Capturing back key won't solve this. Activity Life cycle in android is best left to the System. System will kill your activity whenever it wants.