【问题标题】:How to dismiss the ProgressDialog?如何关闭 ProgressDialog?
【发布时间】:2018-01-19 11:07:30
【问题描述】:

我在解雇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。

【问题讨论】:

    标签: android android-fragments progressdialog


    【解决方案1】:

    您已经为ProgressDialog 创建了新实例,所以现在有两个 ProgressDialog。而且您在loadProgressDialoges() 中已将其创建为本地,因此您无法将其关闭。

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

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

    【讨论】:

    • 如果你使用上面的代码,那么它应该是 dismiss 。不要在本地创建ProgressDialog
    • 当我不断回到这个片段时,第一次进度对话框被关闭,第二次对话框没有被关闭。
    • 我的问题是当我两次返回同一个片段时,第一次对话框被关闭,第二次对话框没有被关闭。
    • 你在某个地方搞砸了生命周期。
    • 你是对的,但我不知道为什么会出现问题以及如何解决?
    猜你喜欢
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 2013-09-27
    • 2011-06-13
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    相关资源
    最近更新 更多