ProgressDialog won't show, even in onPreExecute of AsyncTask

Posted by Geltrude on Stack Overflow See other posts from Stack Overflow or by Geltrude
Published on 2011-02-15T21:51:54Z Indexed on 2011/02/15 23:25 UTC
Read the original article Hit count: 177

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

© Stack Overflow or respective owner

Related posts about android

Related posts about intent