ProgressDialog created from onCreateDialog stops animation on second run

I create a ProgressDialog in onCreateDialog() like this:

 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 absence), decides to cache every dialog created using onCreateDialog (). Because of this, any subsequent call to showDialog(DIALOG_PROGRESS_ID) causes the same instance of ProgressDialog to be used, but the animation has stopped working.

I tried to override the ambiguity in onPrepareDialog() , but that does nothing. There is also no obvious method of invoking an instance of a dialog 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 ProgressBar itself and start reviving it? 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 that didn't work either!

So, does anyone know if there is a way to restart the animation in ProgressDialog? If not, is there a way to force every call to showDialog () to call callCreateDialog ()? (@TuomasR answered this second question, but thinking about it, I don't think this is a very good solution to my problem)

+6
android progressdialog
source share
4 answers

Ha! Got it ... also struggled with it. But the challenge:

 removeDialog(DIALOG_PROGRESS_ID) 

right after

 dismissDialog(...) 

removes it from the (intended) dialog cache for Activity and calls the onCreateDialog call. Create a new ProgressDialog in onCreateDialog and the spinner will animate every time (for me at least).

+15
source share

I do not like to remove Dialog in order to recreate it at the next show. So, I solve this problem using onCreateDialog and onPrepareDialog:

1) In onCreateDialog, I usually create a ProgressDialog. 2) In onPrepareDialog, I reference the progressBar inside it and force 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; } } 
+1
source share

Well, a not-so-cool workaround will allow you to edit the parameters and not declare int private, as in the examples. Of course, you lose the ability to enable onCreateDialog, but you still don't do this:

 showDialog(++DIALOG_PROGRESS_ID); 

Of course, if a dialog is displayed many times, you may have memory errors. Not beautiful, but should work.

0
source share

You can also try.

 if (progressDialog != null) { progressDialog.dismiss(); progressDialog = null; } 
0
source share

All Articles