简体   繁体   中英

ProgressDialog won't show, even in onPreExecute of AsyncTask

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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