Android,单击对话框后退按钮时如何关闭应用程序?
在我的主要活动中,我调用了一个可取消的对话框。当用户在未连接到互联网的情况下使用应用程序时,我会显示此对话框。因此,我显示此对话框要求他连接到互联网。
该对话框没有按钮,我想在用户单击后退按钮时关闭应用程序。在 onKeyDown()
方法中,我正在关闭应用程序 (this.finish();
),但问题是当用户单击后退按钮时屏幕上显示对话框,该对话框消失,我的主要活动是显示。
看来 onKeyDown() 只适用于主要活动而不适用于对话框。当显示对话框并且用户单击后退按钮时如何关闭我的应用程序?
谢谢
=====> 更新
这是我的自定义对话框的代码:
private void initWarningDialog(){
dialogWarning = new Dialog(DRMActivity.this, R.style.customDialogStyle);
dialogWarning.setTitle("Warning!");
dialogWarning.setContentView(R.layout.dialogwarning);
dialogWarning.setCancelable(true);
}
In my main activity, i call a dialog which is cancelable. I show this dialog when user lunches the app while he is not connected to Internet. therefore, I show this dialog to ask him connect to Internet.
This dialog doesn't have button and I want to close application when user clicks back button. In onKeyDown()
method, I'm closing the application (this.finish();
) but the problem is when dialog is displaying on screen when user clicks back button, this dialog disappear and my main activity is show.
It seems onKeyDown() just work in main activity not for dialog. How to close my app when dialog is display and user clicks back button?
Thanks
=====>
Update
This is the code of my custom dialog:
private void initWarningDialog(){
dialogWarning = new Dialog(DRMActivity.this, R.style.customDialogStyle);
dialogWarning.setTitle("Warning!");
dialogWarning.setContentView(R.layout.dialogwarning);
dialogWarning.setCancelable(true);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅调用 finish() 方法:
Update:
当您有自定义对话框时,我建议您设置 OnCancelListner,例如:
Update-2:
这真的很烦人,因为用户如何知道如果他按下后退键,则实际活动将关闭。相反,您应该至少提供“确定”按钮来让用户单击。
Only call finish() method:
Update:
As you are having custom dialog, i would suggest you to set OnCancelListner, something like:
Update-2:
It is really annoying because how user can come to know if he press the back key then actual activity will close. Instead you should provide atleast "ok" button to let user click.
使用
setOnCancelListener
为对话框设置OnCancelListener
,并在OnCancelListener的onCancel
方法中完成活动。Set
OnCancelListener
for dialog usingsetOnCancelListener
and finish activity inonCancel
method of OnCancelListener.OnCancelListener
可能很有用。OnCancelListener
could be useful.