如何解雇ProgressDialog?

时间:2018-01-19 11:07:30

标签: android android-fragments progressdialog

我在解雇ProgressDialog时遇到了问题。当我用容器中的另一个片段替换片段时,片段调用两次,并且ProgressDialog`不会被解雇。

new AlertDialog.Builder(getActivity())
.setTitle("Transfer Status")
.setMessage(msg)
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        Bundle args = new Bundle();
        args.putString("number", mobNumber);
        args.putString("rno", rnoValue);
        args.putInt("count",1);
        getFragmentManager().popBackStack(Fragment_New_Money_Transfer.class.getSimpleName(),
        FragmentManager.POP_BACK_STACK_INCLUSIVE);
        Fragment_New_Money_Transfer fragment = new Fragment_New_Money_Transfer();
        android.support.v4.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragment.setArguments(args);
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        //     fragmentTransaction.addToBackStack(Fragment_Money_Transfer.class.getSimpleName());
        fragmentTransaction.commit();
        dialog.dismiss();
    }
}).show();

上面的代码用于替换片段。

以下代码用于加载和解除对话框

public ProgressDialog loadProgressDialoges() {
    ProgressDialog pDialog = new ProgressDialog(context);
    pDialog.setMessage("Processing...");
    pDialog.isIndeterminate();
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
    return pDialog;
}

public void dismissProgressDialog(ProgressDialog pDialog) {
    if (pDialog != null) {
        pDialog.dismiss();
        pDialog = null;
    }
//     pDialog.dismiss();
}

请帮我解决这个问题。

这是我调用进度对话框的函数

public void getTransferList(){
  showProgress();
        Ion.with(this)
                .load(HelperClass.SERVER_ID + HelperClass.postApis+"/mtvaliatemobileno")
                .setTimeout(HelperClass.timeOut)
                . setHeader(HelperClass.authName,authToken)
                .setHeader(HelperClass.contentName,HelperClass.contentValue)
                .setHeader(HelperClass.secretKeyName,newEncryptedSecretNumber)
                .setHeader(HelperClass.apiKeyName,encryptedDeviceId)
                .setJsonObjectBody(json)
                .asJsonObject()
                .withResponse()
                .setCallback(new FutureCallback<Response<JsonObject>>() {
                    @Override
                    public void onCompleted(Exception e, Response<JsonObject> result) {
                        dismissDialog();
                        if (e != null) {
                            e.printStackTrace();
                            Toast.makeText(getActivity(), "Connection Failed", Toast.LENGTH_SHORT).show();

                        } else {
                            if (result != null) {

                                try{
                                    Boolean responceMessage = result.getResult().get("res").getAsBoolean();

                                    JsonObject jsonObject1 = result.getResult().get("CardDetail").getAsJsonObject();
}
}
}

以下代码用于显示和关闭进度对话框

 public void showProgress() {
    if (pDialog == null) {
        pDialog = new ProgressDialog(getActivity());
    }
    pDialog.setMessage("Processing...");
    pDialog.setCancelable(true);
    pDialog.show();
}
public void dismissDialog() {
    if (pDialog != null && pDialog.isShowing())
        pDialog.dismiss();
}

我已在全球宣布了ProgressDialog。

1 个答案:

答案 0 :(得分:0)

您已为ProgressDialog创建新实例,因此现在有两个ProgressDialog。而且你在loadProgressDialoges()里面你已经在本地创建了它,所以你无法解雇它。

解决方案; - 只需全局使用Single实例。

private ProgressDialog pDialog;
    public void showProgress() {
        if (pDialog == null) {
            pDialog = new ProgressDialog(this);
        }
    pDialog.setCancelable(false);
    pDialog.show();
    }
    public void dismissDialog() {
        if (pDialog != null && pDialog.isShowing())
            pDialog.dismiss();
    }