如何在backpress上退出android应用程序? [英] How to exit the android application on backpress?

查看:358
本文介绍了如何在backpress上退出android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况可能是说我有5个活动。说出主要活动,活动1,活动2,活动3和活动4。

The situation could be like say, I've 5 activities. Say Main activity, Activity 1, Activity 2, Activity 3 and Activity 4.

一个人可以使用活动1,2,3&

One can use the Activity 1,2,3 & 4 directly with the help of buttons so inserted on Main Activity layout.

情况:用户进入主要活动,然后按一个按钮进入活动2。返回并返回主要活动。现在,他从那里通过另一个按钮进入活动3,同样,当他按下后退按钮时,他也进入了主要活动。

Situation: User enters into the Main Activity, and presses a button to enter into Activity 2. Then he back presses and comes back to Main Activity. from there he now enters Activity 3 with the help of another button and similarly when he presses back button he enters into Main Activity.

主要活动->活动2->主要活动->活动3->主要活动。

问题:现在,当用户从活动3进入主要活动时。用户位于主要活动布局上。现在,如果用户按下手机上的后退按钮,则将通过以下过程退出该应用程序:

Problem: Now as the user enters from Activity 3 to Main Activity. The user is on Main Activity layout. And now if the user presses the back button on its phone, then it will take the following process to exit the app:

主要活动->活动3 ->主要活动->活动2->主要活动->退出

需要完成的工作:我想从退出应用程序主要活动本身,如果用户按下手机上的后退按钮,那么我希望用户应该能够通过显示以下内容退出主要活动本身,而不是按照上述路径退出应用简单的AlertDialog对话框

Needs to be done: I want to exit the app from the Main Activity itself, if the user presses the back button on its phone, then instead of following the above path to exit the app, I want that the user should be able to exit it from the Main Activity itself by displaying a simple AlertDialog box.

由于找不到解决方案,该怎么办?

How could that be done, as I'm unable to find a solution to this problem?

推荐答案

您需要覆盖<$ c $中的 onBackPressed() c> MainActivity()
并使用 finishAffinity 完成所有活动

You need to Override onBackPressed() inside your MainActivity() and use finishAffinity to finish all activities

onBackPressed()


在活动检测到用户按下返回键时调用。

Called when the activity has detected the user's press of the back key.

finishAffinity()
已添加到API级别16

finishAffinity() : added in API level 16


完成此活动以及位于其下方的所有活动具有相同亲和力的当前任务。

Finish this activity as well as all activities immediately below it in the current task that have the same affinity.

示例代码

@Override
public void onBackPressed() {
  super.onBackPressed();
  ActivityCompat.finishAffinity(MainActivity.this);
}



编辑



EDIT


我希望用户可以通过显示一个简单的AlertDialog框将其从Main Activity本身退出。

I want that the user should be able to exit it from the Main Activity itself by displaying a simple AlertDialog box.



@Override
public void onBackPressed() {

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
                .setTitle("Alert")
                .setCancelable(false)
                .setMessage("Are your sure want to exit?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.finishAffinity(MainActivity.this);
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

        builder.show();

}

这篇关于如何在backpress上退出android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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