Android,单击对话框后退按钮时如何关闭应用程序?

发布于 2024-12-23 05:38:47 字数 667 浏览 2 评论 0原文

在我的主要活动中,我调用了一个可取消的对话框。当用户在未连接到互联网的情况下使用应用程序时,我会显示此对话框。因此,我显示此对话框要求他连接到互联网。

该对话框没有按钮,我想在用户单击后退按钮时关闭应用程序。在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里兽 2024-12-30 05:38:47

仅调用 finish() 方法:

dialog.setPositiveButton("Ok",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // Finish activity
            finish();
        }
    });

Update:

当您有自定义对话框时,我建议您设置 OnCancelListner,例如:

dialogWarning.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                // TODO Auto-generated method stub                  

                finish();
            }
 });

Update-2:

这真的很烦人,因为用户如何知道如果他按下后退键,则实际活动将关闭。相反,您应该至少提供“确定”按钮来让用户单击。

Only call finish() method:

dialog.setPositiveButton("Ok",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // Finish activity
            finish();
        }
    });

Update:

As you are having custom dialog, i would suggest you to set OnCancelListner, something like:

dialogWarning.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                // TODO Auto-generated method stub                  

                finish();
            }
 });

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.

泡沫很甜 2024-12-30 05:38:47

使用setOnCancelListener为对话框设置OnCancelListener,并在OnCancelListener的onCancel方法中完成活动。

Set OnCancelListener for dialog using setOnCancelListener and finish activity in onCancel method of OnCancelListener.

衣神在巴黎 2024-12-30 05:38:47

OnCancelListener 可能很有用。

 alert.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                // TODO Auto-generated method stub                  

                finish();
            }
 });

OnCancelListener could be useful.

 alert.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                // TODO Auto-generated method stub                  

                finish();
            }
 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文