在 Android 中按下后退按钮时保持我的活动
我正在实现这个应用程序,当按下后退键时我需要暂停它或显示一个对话框来询问用户是否真的想退出。
我确实覆盖了后退键以显示带有 2 个按钮的对话框,是并取消,但活动无论如何都已完成,而没有显示对话框
我用来覆盖后退键的代码如下
// Overriding The Back Key To Stop The Music If Running
@Override
public void onBackPressed() {
super.onBackPressed();
show_are_you_sure_exit();
}
,函数如下
public void show_are_you_sure_exit() {
final AlertDialog show_are_you_sure_delete = new AlertDialog.Builder(
this).create();
show_are_you_sure_delete
.setMessage("Are You Sure You Want To Exit?! Your Pregress Won't Be Saved");
show_are_you_sure_delete.setButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface StopActivityDialog,
int which) {
start_main_screen_intent();
finish();
}
});
show_are_you_sure_delete.setButton2("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
show_are_you_sure_delete.show();
}
: start_main_screen_intent( )开始另一项活动,
感谢您的帮助:)
I'm implementing this application where i need to pause it when the back key is pressed or show a dialog to ask the user if he really wants to exit.
I did override the back key to show a dialog with 2 button, yes and cancel, but the activity is finished anyways without showing the dialog
the code I am using to override the back key is the following
// Overriding The Back Key To Stop The Music If Running
@Override
public void onBackPressed() {
super.onBackPressed();
show_are_you_sure_exit();
}
and the function is the following
public void show_are_you_sure_exit() {
final AlertDialog show_are_you_sure_delete = new AlertDialog.Builder(
this).create();
show_are_you_sure_delete
.setMessage("Are You Sure You Want To Exit?! Your Pregress Won't Be Saved");
show_are_you_sure_delete.setButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface StopActivityDialog,
int which) {
start_main_screen_intent();
finish();
}
});
show_are_you_sure_delete.setButton2("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
show_are_you_sure_delete.show();
}
with start_main_screen_intent() starts another activity
thx for your help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在调用 super.onBackPressed() 来调度事件并终止活动
你想要的是
这样的方式 你以你自己的方式处理事件,而不是正常的方式
your are calling super.onBackPressed() which dispatches the event up and kills the activity
what you want is
this way YOU handle the event your way, not the normal way
尝试删除 super.onBackPressed() 调用。在这种情况下,您可能还需要删除 @Override,否则 Eclipse 可能会对您大喊大叫。我认为正在发生的事情是基础实施正在结束该活动。
Try removing the super.onBackPressed() call. You may also need to remove the @Override in this case or Eclipse might yell at you. I think what is happening is the base implementation is ending the activity.
不要调用
super.onBackPressed();
。它所做的唯一事情(如果您扩展Activity
)就是调用finish()
。然而,请注意这个“退出”功能并非来自 Android 世界。我建议检查您的应用程序架构以摆脱退出概念。
您可以在这篇文章中找到有关“退出”为何不好的详细信息:退出应用程序会让人皱眉吗?
Don't call
super.onBackPressed();
. The only thing it does (if you extendActivity
) is it callsfinish()
.However, note this 'exit' feature is smth not from Android world. I'd recommend to review your app architecture to get rid of exit concept.
You can find details on why 'exit' is bad in this post: Is quitting an application frowned upon?