开发者

ProgressDialog won't show, even in onPreExecute of AsyncTask

开发者 https://www.devze.com 2023-02-11 10:43 出处:网络
In my class, Main extends Activity, I\'ve this: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {

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 SetStatus开发者_如何学编程ProgressBar(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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号