How to dismiss the ProgressDialog?

跟風遠走 提交于 2019-12-31 06:48:21

问题


I am facing the issue in dismissing the ProgressDialog. When I replace the fragment with another fragment in a container, the fragment is calling twice and ProgressDialog` is not dismissing.

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

The above code is for replacing the fragment.

The below code is for loading and dismissing dialog

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

Please help me how to solve this.

This is my function calling the progress dialog

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

The below code is for show and dismiss progress dialog

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

I have declared the ProgressDialog Globally.


回答1:


You have create new instance for ProgressDialog so now there is two ProgressDialog. And also you inside loadProgressDialoges() you have created it as locally so there is no way you can dismiss it.

Solution;- Just use a Single instance globally .

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


来源:https://stackoverflow.com/questions/48339724/how-to-dismiss-the-progressdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!