简体   繁体   English

如何在 AsyncTask 中关闭 ProgressDialog

[英]How can I dismiss ProgressDialog in AsyncTask

I am using a ProgressDialog to be shown while my background process goes on, but after background process is completed the ProgressDialog is not dismissed still.我正在使用ProgressDialog在后台进程进行时显示,但是在后台进程完成后 ProgressDialog 仍然没有被关闭。

Here is my code这是我的代码

private class async extends AsyncTask<String, Void, Boolean> {
    final ProgressDialog progressDialog = new ProgressDialog(getParent());

    @Override
    protected Boolean doInBackground(String... params) {

        GetJson json = new GetJson();
        boolean success = false;

        JSONObject mJsonObject = json
            .readJsonObject("url");
        try {
            success = mJsonObject.getBoolean("success");
        } catch (Exception e) {
        }
        return success;
    }

    @Override
    protected void onPostExecute(Boolean result) {

        if (result) {
            if (progressDialog.isShowing())
                progressDialog.dismiss();
            }
        }
    }

    @SuppressWarnings("static-access")
    @Override
    protected void onPreExecute() {
        progressDialog.show(getParent(), "Working..", "Please wait...");
    }
}
private final class YourTask extends AsyncTask<Void, Void, Object> {
    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(YourActivity.this, "Title", "Message", true);
    }

    @Override
    protected Object doInBackground(final Void... params) {
        // Doing something
    }

    @Override
    protected void onPostExecute(final Object result) {
        // Check result or something
        dialog.dismiss();
    }

}

In onPostExecute() method call dismiss() on your dialog.onPostExecute()方法中调用您的对话框上的dismiss()

You can call progressDialog.dismiss() in your AsyncTask 's onPostExecute() method.您可以在AsyncTaskonPostExecute()方法中调用progressDialog.dismiss()

I dont know, if you solved this problem, probably yes.我不知道,如果你解决了这个问题,可能是的。

But for next users what will have the same problem (i had it right now too)..但是对于下一个用户会有同样的问题(我现在也有)..

The problem is in your declaration.问题出在您的声明中。

final ProgressDialog progressDialog = new ProgressDialog(getParent());

or in your "second" declaration或在您的“第二个”声明中

progressDialog.show(getParent(), "Working..", "Please wait...");

Because in the first one you put in there the getParent parameter (probably MainActivity.this) and the same you do in calling show method.因为在第一个中,您在其中放入了 getParent 参数(可能是 MainActivity.this),并且您在调用 show 方法时所做的相同。 Therefore is there 2 parents.. and if you call dismiss() in post execute..it dismiss the first one..but not the another one what have then dialog.isShowing() equal to false.因此,是否有 2 个父母.. 如果您在执行后调用 dismiss().. 它会关闭第一个.. 但不是另一个,然后 dialog.isShowing() 等于 false。

So important is have there just 1..:!!如此重要的是只有 1 ..:!! parent content..so you can assign the content in declaration with:父内容..所以你可以在声明中分配内容:

new ProgressDialog(content);

or you can do或者你可以做

ProgressDialog dialog=new ProgressDialog();
dialog.setMessage("Loading");
dialog.show(getParent());

and then dismiss it and everything is allright.然后解雇它,一切都很好。 or you can do:或者你可以这样做:

ProgressDialog dialog=new ProgressDialog(getParent());
dialog.setMessage("Loading");
dialog.show();

but never give in there twice parents, contents, whatever..但永远不要在那里两次放弃父母,内容,无论如何..

AsyncTasks should not handle at ALL a dialog. AsyncTasks 根本不应该处理一个对话框。 Dismissing the dialog in the PostExecute phase can easily lead to an IllegalStateException as the underlying activity can already be destroyed when the dialog gets dismissed.在 PostExecute 阶段关闭对话框很容易导致 IllegalStateException,因为当对话框被关闭时,底层活动可能已经被破坏。 Before destroying the activity the state of the activity will be saved.在销毁活动之前,活动的 state 将被保存。 Dismissing the dialog afterwards will lead to an inconsistent state.之后关闭对话框将导致 state 不一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM