即使在 AsyncTask 的 onPreExecute 中,ProgressDialog 也不会显示

发布于 2024-10-17 16:56:28 字数 1717 浏览 5 评论 0原文

在我的类中,Main 扩展了 Activity,我有这个:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case ...
    case CREDENTIAL_VIEW:
        new SetStatusProgressBar(this).execute();

还有一个嵌套类:

private class SetStatusProgressBar extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog dialog;
    private Main ctx;

    public SetStatusProgressBar(Main ctx) {
        this.ctx = ctx;
        dialog = new ProgressDialog(ctx);
    }

    // progress dialog to show user that contacting server.
    protected void onPreExecute() {
        this.dialog = ProgressDialog.show(ctx, null,
                "Refreshing data from server...", true, false);
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        //...
        //statements that refresh UI
        //...

        if (dialog.isShowing()) {
            dialog.dismiss();
            timerProgressBarStop();
        }
    }

    protected Boolean doInBackground(final String... args) {
        //...
        //statements to download data from server
        //...
        return true;
    }

}

在 Main 类中,我打开第二个 Activity,以这种方式:

Intent myIntent = new Intent(Main.this, Credentials.class);
startActivityForResult(myIntent, CREDENTIAL_VIEW);

第二个 Activity 以这种方式返回到 Main 活动:

Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();

我不理解为什么当我从第二个 Activity 导航到 Main 时,进度对话框只会在 UI 刷新后显示...这样,进度对话框仅在屏幕上停留半秒...然后隐藏! :( 我想在整个下载过程中看到顶部的进度对话框!

请帮忙。 谢谢大家

In my class, Main extends Activity, I've this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case ...
    case CREDENTIAL_VIEW:
        new SetStatusProgressBar(this).execute();

And there is this nested class:

private class SetStatusProgressBar extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog dialog;
    private Main ctx;

    public SetStatusProgressBar(Main ctx) {
        this.ctx = ctx;
        dialog = new ProgressDialog(ctx);
    }

    // progress dialog to show user that contacting server.
    protected void onPreExecute() {
        this.dialog = ProgressDialog.show(ctx, null,
                "Refreshing data from server...", true, false);
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        //...
        //statements that refresh UI
        //...

        if (dialog.isShowing()) {
            dialog.dismiss();
            timerProgressBarStop();
        }
    }

    protected Boolean doInBackground(final String... args) {
        //...
        //statements to download data from server
        //...
        return true;
    }

}

In the Main class I open a second Activity, in this way:

Intent myIntent = new Intent(Main.this, Credentials.class);
startActivityForResult(myIntent, CREDENTIAL_VIEW);

That second Activity returns to the Main activity in this way:

Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();

I don't understand why when I navigate from the second Activity to the Main, the ProgressDialog will show ONLY AFTER that the UI refreshes... In this way the Progress Dialog stays on the screen only for half second... and then hides! :( I'd like to see the ProgressDialog on top during all the download time!

Help, please.
Thank you all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-10-24 16:56:28

好的,首先使用 getApplicationContext() 而不是 ctx 变量。使用“this”不适合记忆力好的公民。
尝试在 onProgressUpdate(...) 方法中更新 ProgressDialog。

Ok, first of all use getApplicationContext() instead of ctx variable. Using 'this' is not for good memory citizens.
Try updating progressDialog in onProgressUpdate(...) method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文