关闭背面的关闭活动和对话框

时间:2014-03-06 11:51:28

标签: android progressdialog

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

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

我已经实施了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();

6 个答案:

答案 0 :(得分:2)

通过按下

自动关闭OnStop()休息Activity finish()对话框中的对话框
@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    if(progressDialog.isShowing()) {
        progressDialog.dismiss();
    }
}

快乐编码!!

答案 1 :(得分:2)

pDialog.setCancelable(true);

其余的代码都没问题。

答案 2 :(得分:1)

使用此方法,无需为活动调用结束:

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

答案 3 :(得分:1)

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

<强>步骤1 改变

pDialog.setCancelable(false)

pDialog.setCancelable(true)

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

<强>步骤-2

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

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

            }

        });

答案 4 :(得分:0)

关闭对话框的完成活动:

pDialog .setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {
                finish();
            }
        });

答案 5 :(得分:0)

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

选项1

创建进度对话框

master

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

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...

on backpressed

 dialog.dismiss();

选项2

创建全局变量

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

创建进度对话框

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();

on backpressed

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