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

时间:2011-02-15 21:51:54

标签: android android-intent show android-asynctask progressdialog

在我的班级中,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时,ProgressDialog只会在UI刷新后显示...这样进度对话框只停留在屏幕上半秒钟......然后隐藏! :(我想在所有下载时间内看到ProgressDialog在顶部!

请帮忙。 谢谢大家

1 个答案:

答案 0 :(得分:-1)

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