离开与返回按钮Android应用程序 [英] Leaving android app with back button

查看:134
本文介绍了离开与返回按钮Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的Andr​​oid应用程序的用户离开我的应用程序时,preSS回来一定的活动。可以这样做?

I want the users of my android app to leave my app when they press back at a certain activity. Can this be done?

推荐答案

您可以尝试这样的事情(显示一个对话框,以确认退出):

You can try something like this (shows a dialog to confirm exit):

        @Override
        public void onBackPressed() {
                    new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
                            .setMessage("Are you sure you want to exit?")
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    finish();
                                    System.exit(0);
                                }
                            }).setNegativeButton("No", null).show();
                }

必须指出,因为它是在提到下面在离开与System.exit一个应用程序的注释不推荐。一个更正确的方式很可能是广播上回pressed的意图,如果您的应用程序的活动,收到的意图完成自己。

It must be noted as it is mentioned in the comments below that exiting an app with System.exit is not recommended. A more "correct" way would probably be to broadcast an intent on back pressed and if the activities of your application received that intent finish themselves.

这篇关于离开与返回按钮Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆