ProgressDialog created from onCreateDialog stops animating on second run
Asked Answered
S

4

6

I create a ProgressDialog in onCreateDialog() like so:

protected Dialog onCreateDialog(int id) {
  if (id == DIALOG_PROGRESS_ID)
  {
      ProgressDialog dialog = new ProgressDialog(this);
      dialog.setMessage(getResources().getString(R.string.MyLabel));
      dialog.setCancelable(false);
      dialog.setIndeterminate(true);
      return dialog;
  }
}

Android, in its wisdom (or serious lack of it) decides to cache every dialog created through onCreateDialog(). Because of that, any subsequent call to showDialog(DIALOG_PROGRESS_ID) results in the same ProgressDialog instance being used but the animation has stopped working.

I've tried to re-set indeterminate in onPrepareDialog(), but that doesn't do anything. There is likewise no obvious method to call on the dialog instance that will reset the animation.

protected void onPrepareDialog(int id, Dialog dialog)
{
  //This doesn't do anything
  if (id == DIALOG_PROGRESS_ID)
     ((ProgressDialog)dialog).setIndeterminate(true);
  super.onPrepareDialog(id, dialog);
}

EDIT: But maybe there is a way to get the ProgressBar itself and start it animating? so I tried the following after I asked this question:

@Override
protected void onPrepareDialog(int id, Dialog dialog)
{
  if (id == DIALOG_PROGRESS_ID)
  {
     ProgressBar p = (ProgressBar) dialog.findViewById(android.R.id.progress);
     if (p.getAnimation() != null)
        p.startAnimation(p.getAnimation());
  }
  super.onPrepareDialog(id, dialog);

}

But it didn't work either!

So, does anyone know if there is a way to restart animation on a ProgressDialog? If not, is there a way that I can force every showDialog() call to call onCreateDialog()? (this second question was answered by @TuomasR, but after pondering it I don't think this is a very good solution to my problem)

Saltillo answered 29/9, 2010 at 11:49 Comment(0)
U
15

Ha! Got it... was also struggling with this. But calling:

removeDialog(DIALOG_PROGRESS_ID)

immediately after

dismissDialog(...)

removes it from the (presumed) dialog cache for the Activity and forces a call to onCreateDialog. Create a new ProgressDialog in onCreateDialog and the spinner animates everytime (for me at least).

Unstrung answered 30/11, 2010 at 10:18 Comment(1)
No need to call dismissDialog method - you can just call removeDialog, since If the dialog is showing, it will dismiss it as part of the clean up (from JavaDoc)Eadith
D
1

I don't like to removeDialog to recreate it on next showing. So, I resolve this issue by using onCreateDialog and onPrepareDialog:

1) In onCreateDialog I normally create the ProgressDialog. 2) In onPrepareDialog I reference the progressBar inside it and force to restart:

@Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
    switch (id){

        .....

        case DIALOG_PROGRESS_ID:
            ProgressBar p = (ProgressBar) dialog.findViewById(android.R.id.progress);
            p.setVisibility(View.GONE);
            p.setVisibility(View.VISIBLE);
        break;
    }
}
Denominative answered 18/7, 2011 at 11:40 Comment(0)
U
1

You can also try.

if (progressDialog != null) {
    progressDialog.dismiss();
    progressDialog = null;
}
Unseemly answered 30/5, 2012 at 8:14 Comment(1)
really best answerEnvelop
W
0

Well, a not-so-cool workaround would to edit the parameters and not declaring the int private as in the examples. Of course, you lose the ability to switch on onCreateDialog, but you don't seem to be doing that anyway:

showDialog(++DIALOG_PROGRESS_ID);

Of course if the dialog is shown numerous times, you could have memory errors. Not pretty, but should work.

Waft answered 29/9, 2010 at 11:57 Comment(1)
I am switching in the actual code, I edited it down to the relevant bits for the question. You raise a good point that I didn't really consider: if we force onCreateDialog() to be called every time, we force Android to cache all these dialogs. Grrr! Android's full of these rough edges.Saltillo

© 2022 - 2024 — McMap. All rights reserved.