【问题标题】:close activity and dialog box on back press按下后关闭活动和对话框
【发布时间】:2014-03-06 11:51:27
【问题描述】:

我有一个带有setCancelable(false) 的进度对话框的活动。

我想完成活动并关闭backPressed() 上的progressdialog box

我已经实现了onBackPressed(),但它不起作用。

@Override
public void onBackPressed() {
    if(pDialog.isShowing()){
        pDialog.dismiss();
    }
    this.finish();
    super.onBackPressed();
}

进度对话框代码是

        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setTitle("Connecting to Server");
        pDialog.setMessage("Updating Assignee Detail. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();

【问题讨论】:

  • 为什么不能在 onBackPressed 方法中创建 pDialog.setCanceleable(true)。 finishonBackPressed 做同样的事情。删除 this.finish();
  • 如果更改为 setCancelable(true) 会发生什么?那你的代码能用吗?
  • 发布您的进度对话框代码。
  • @user57445:看我的回答。测试成功了。

标签: android progressdialog


【解决方案1】:

设置

pDialog.setCancelable(true);

你的其余代码都很好。

【讨论】:

    【解决方案2】:

    只需关闭OnStop() 中的对话框,其余Activity 将通过按返回自动finish()

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        if(progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }
    

    编码愉快!!

    【讨论】:

      【解决方案3】:

      您可以通过以下两个步骤来实现它,

      第 1 步 改变

      pDialog.setCancelable(false)

      pDialog.setCancelable(true)

      因为如果 Dialog 设置为 false,则无法取消它。

      第二步

      为您的Dialog 添加一个监听器,以便在取消时监听,如下所示,

      pDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                  @Override
                  public void onCancel(DialogInterface dialog) {
                      // if from activity
                      finish();
      
                  }
      
              });
      

      【讨论】:

        【解决方案4】:

        使用此方法,无需为activity调用finish:

        @Override
            public boolean onKeyDown(int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    if(null != pDialog)
        pDialog.dismiss();
                }
                return super.onKeyDown(keyCode, event);
            }
        

        【讨论】:

          【解决方案5】:

          intead 在对话框关闭时完成活动:

          pDialog .setOnDismissListener(new OnDismissListener() {
          
                      @Override
                      public void onDismiss(DialogInterface dialog) {
                          finish();
                      }
                  });
          

          【讨论】:

            【解决方案6】:

            这是一个进度对话框的小例子,关闭它和活动...

            选项 1

            创建进度对话框

            ProgressDialog dialog = new ProgressDialog(this); //create progress dialog
                dialog.setMessage("¡wait it, please...!"); //body text...
                dialog.setCancelable(true); //if it will be cancelable
                //this is the listener cancelable
                dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        finish();
                    }
                });
                dialog.show(); //show it...
            

            以某种威胁或任何你想要的方式完成它......

             dialog.dismiss();
            

            反压

            @Override
            public void onBackPressed() {
                if(dialog.isShowing()){
                    dialog.cancel();
                }
                finish();
            }
            

            选项 2

            创建一个全局变量

            Boolean validation=false;
            

            创建进度对话框

            ProgressDialog dialog = new ProgressDialog(this); //create progress dialog
                dialog.setMessage("¡wait it, please...!"); //body text...
                dialog.setCancelable(false); //if it will be cancelable
                //this is the listener dismiss
                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialogInterface) {
                            if(validation){
                                dialogInterface.dismiss();
                            }else{
                                finish();
                            }
                        }
                    });
                dialog.show(); //show it...
            

            完成它

             //if finish process 
             validation=true;
             dialog.dismiss();
            

             //if finish with backpress
             validation=false;
             dialog.dismiss();
            

            反压

            @Override
            public void onBackPressed() {
                if(dialog.isShowing()){
                    validation=false;
                    dialog.dismiss();
                }
                finish();
            }
            

            【讨论】:

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