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

问题描述

情况可能就像说,我有 5 个活动.说主要活动、活动 1、活动 2、活动 3 和活动 4.

可以使用 Activity 1,2,3 &4 直接在按钮的帮助下插入 Main Activity 布局.

情况:用户进入主活动,按下一个按钮进入活动2.然后他回按回到主活动.从那里他现在在另一个按钮的帮助下进入活动 3,同样,当他按下后退按钮时,他进入主活动.

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

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

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

需要完成:我想从主活动本身退出应用程序,如果用户按下手机上的后退按钮,那么不是按照上述路径退出应用程序,我希望用户应该能够通过显示一个简单的 AlertDialog 框从 Main Activity 本身退出它.

这怎么可能,因为我无法找到解决此问题的方法?

解决方案

您需要在 MainActivity() 中覆盖 onBackPressed()并使用 finishAffinity 完成所有活动

onBackPressed()

当活动检测到用户按下返回键时调用.

finishAffinity() :在 API 级别 16 中添加

完成此活动以及当前任务中紧邻其下方的所有具有相同关联的活动.

示例代码

@Override公共无效 onBackPressed() {super.onBackPressed();ActivityCompat.finishAffinity(MainActivity.this);}

编辑

我希望用户能够通过显示一个简单的 AlertDialog 框从 Main Activity 本身退出它.

@Override公共无效 onBackPressed() {AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this).setTitle("警报").setCancelable(false).setMessage("你确定要退出吗?").setPositiveButton("Yes", new DialogInterface.OnClickListener() {@覆盖public void onClick(DialogInterface dialog, int which) {ActivityCompat.finishAffinity(MainActivity.this);}}).setNegativeButton("No", new DialogInterface.OnClickListener() {@覆盖public void onClick(DialogInterface dialogInterface, int i) {}});builder.show();}

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

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

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.

Main Activity --> Activity 2 --> Main Activity --> Activity 3 --> Main Activity.

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:

Main Activity --> Activity 3 --> Main Activity --> Activity 2 --> Main Activity --> EXIT

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?

解决方案

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

onBackPressed()

finishAffinity() :added in API level 16

SAMPLE CODE

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

EDIT

@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应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 01:20