退出Android应用程序无法正常工作

时间:2016-01-20 06:56:31

标签: android dialog

在我的Android应用程序中,当我从HomeAcitvity按下时,我希望从应用程序退出。但是当我按下后,它总是转到Loginactivity。

以下HomeAcitvity中使用的代码

 @Override
    public void onBackPressed() {
        android.app.AlertDialog.Builder alertDialog = new android.app.AlertDialog.Builder(HomeActivity.this);

        alertDialog.setTitle("Exit"); // Sets title for your alertbox

        alertDialog.setMessage("Are you sure you want to Exit ?"); // Message to be displayed on alertbox

        alertDialog.setIcon(R.drawable.logout_icon); // Icon for your alertbox

/* When positive (yes/ok) is clicked */
        alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {

                finish();
                System.exit(0);
            }
        });

/* When negative (No/cancel) button is clicked*/
        alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        alertDialog.show();
    }

帮助解决这个问题

5 个答案:

答案 0 :(得分:1)

试试这种方式

在呼叫意图家庭活动finish();

之后的登录活动中
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
finish();

以及退出

所需的活动
@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();
 }

快乐的措辞..

答案 1 :(得分:0)

在您的代码

中使用此功能
public void onClick(DialogInterface dialog,int which) {
 moveTaskToBack(true);
 android.os.Process.killProcess(android.os.Process.myPid());
 System.exit(1);
 finish();
 }

答案 2 :(得分:0)

在启动Home活动之前,清除堆栈,如下所示。

Intent intent_home = new Intent(this, HomeActivity.class);
intent_home.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent_home);

答案 3 :(得分:0)

从登录活动

启动家庭活动时,请使用以下代码
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(inputString);
if (matcher.find()) {
    String email = inputString.substring(matcher.start(), matcher.end());
} else {
    // TODO handle condition when input doesn't have an email address
}

上面的代码将清除之前打开的所有活动的后台堆栈。并且将在没有先前/父活动的情况下启动HomeActivity。而且你也不需要覆盖onBackPress方法。

答案 4 :(得分:0)

System.exit()和finish()在android 4.4.2上面表面上相似。在此之前,System.exit()不会在finish()时调用onActivityResult()方法。否则,他们都结束了当前的活动。此外,finish()似乎比System.exit()工作得快。

我认为你需要写下这个:

android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0)                   // you may also use finish() or nothing as per your requirements