【问题标题】:showing alert dialog on pressing back button [duplicate]在按下后退按钮时显示警报对话框[重复]
【发布时间】:2016-05-17 12:29:20
【问题描述】:

我想在按下 Android 设备的后退按钮时显示一条警报消息,询问用户是否确定天气。如果用户按“是”,那么他应该导航到上一个。如果出现“否”,则应恢复活动。但是当我按下后退按钮时会出现问题,它会执行这两个操作。它还导航到以前的活动并显示警报对话框。

这是我的代码。

public void onBackPressed() {

    super.onBackPressed();

    AlertDialog.Builder alertdialog=new AlertDialog.Builder(this);
    alertdialog.setTitle("Warning");
    alertdialog.setMessage("Are you sure you Want to exit the tutorial???");
    alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent=new Intent(secAddition.this,addition.class);
            startActivity(intent);
            secAddition.this.finish();
        }
    });

    alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    AlertDialog alert=alertdialog.create();
    alertdialog.show();

}

【问题讨论】:

  • 不要在 BackPress 上调用 startActivity。你应该打电话给超级 OnBackPress。当按下硬件返回时。检查我的答案。

标签: java android android-studio


【解决方案1】:

如果您想在不关闭应用或清除堆栈的情况下显示警报对话框,则不应调用 super.onBackPressed();

【讨论】:

  • 答案简单、简短且正确
【解决方案2】:

请检查以下代码sn-p

 @Override
 public void onBackPressed() {
     //super.onBackPressed();
     IsFinish("Want to close app?");
 }

 public void IsFinish(String alertmessage) {

      DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                    switch (which) {
                        case DialogInterface.BUTTON_POSITIVE:
                            android.os.Process.killProcess(android.os.Process.myPid());
                            // This above line close correctly
                            //finish();
                            break;

                        case DialogInterface.BUTTON_NEGATIVE:  
                            break;
                    }
                }
            };

      AlertDialog.Builder builder = new AlertDialog.Builder(context);
      builder.setMessage(alertmessage)
                .setPositiveButton("Yes", dialogClickListener)
                .setNegativeButton("No", dialogClickListener).show();

 }

【讨论】:

    【解决方案3】:

    这就是你要找的。您不需要在后按时启动活动。 如果用户选择是而不是调用 super onBackPress

    public void onBackPressed() {
    
       AlertDialog.Builder alertdialog=new AlertDialog.Builder(this);
       alertdialog.setTitle("Warning");
       alertdialog.setMessage("Are you sure you Want to exit the tutorial???");
       alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener(){
    
          @Override
          public void onClick(DialogInterface dialog, int which) {
             super.onBackPressed();
          }
        });
    
        alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener(){
    
          @Override
          public void onClick(DialogInterface dialog, int which) {
             dialog.cancel();
          }
    
        });
    
        AlertDialog alert=alertdialog.create();
        alertdialog.show();
    
    }
    

    【讨论】:

      【解决方案4】:

      删除

      super.onBackPressed();
      

      这应该可行。

      public void onBackPressed() {
      
         AlertDialog.Builder alertdialog=new AlertDialog.Builder(this);
         alertdialog.setTitle("Warning");
         alertdialog.setMessage("Are you sure you Want to exit the tutorial???");
         alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener(){
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 Intent intent=new Intent(secAddition.this,addition.class);
                 startActivity(intent);
                 secAddition.this.finish();
             }
          });
      
         alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                  dialog.cancel();
             }
         });
      
         AlertDialog alert=alertdialog.create();
         alertdialog.show();
      
      }
      

      【讨论】:

      • 成功了.. 谢谢
      • 请标记为答案。
      【解决方案5】:

      你可以这样写让你的代码工作:

      public void onBackPressed() {
      
           AlertDialog.Builder alertdialog=new AlertDialog.Builder(this);
           alertdialog.setTitle("Warning");
           alertdialog.setMessage("Are you sure you Want to exit the tutorial???");
           alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialog, int which) {
                       super.onBackPressed();    
                 }
           });
      
          alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener(){
                 @Override
                 public void onClick(DialogInterface dialog, int which) {
                       dialog.cancel();
                 }
          });
      
          AlertDialog alert=alertdialog.create();
          alertdialog.show();
      
      }
      

      如果你要去以前的Activity,不需要再次启动Activity

      在您的代码中,当您进行后按时,super.onBackPressed() 会被调用,因此它会转到之前的 Activity

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多