Android: Progress Dialog spinner not spinning

2019-01-18 22:32发布

This seems to be an Android-wide problem, which you can see in API demos under Views -> Progress Bar -> Dialogs.

Basically, if you show a progress dialog, it works as expected the first time around. If you dismiss it and show it again (without destroying the activity or anything like that), the spinning image stops spinning. In the API Demo you can see this by clicking "Show Indeterminate", pressing back to dismiss the dialog, and clicking the button again.

I've tried constructing my own progress dialog, however it shows the same problem, as the problem is with the "ProgressBar" spinning image.

I am wondering if anyone has a workaround for this problem.

5条回答
▲ chillily
2楼-- · 2019-01-18 22:50

You don't have to use onCreateDialog for ProgressDialog objects, as explained here http://developer.android.com/guide/topics/ui/dialogs.html#ProgressDialog . You should create and show the progress dialog in the main code instead of using showDialog and onCreateDialog.

查看更多
看我几分像从前
3楼-- · 2019-01-18 23:07

You can define class and implement the AsyncTask.There you define your background processes in the doinbackground() method like this.

 @Override
   protected String doInBackground(String... params) {
   publishProgress(new Integer(1));
   for(int i=0;i<=300;i++){
System.out.println(i);
}
publishProgress(new Integer(1));
for(int i=301;i<=600;i++){
System.out.println(i);
}
 .................
 ....................
 ...........................
 //your background tasks
publishProgress(new Integer(1));
for(int i=601;i<=900;i++){
System.out.println(i);
}
pd.dismiss();//dismiss the progress dialog here...
return null;
}

You can also show your progress information by the onProgressUpdate() method.Check a full easy to follow tutorial at http://www.androidcookers.co.cc/2011/12/want-to-run-progress-dialog-in-android.html

查看更多
疯言疯语
4楼-- · 2019-01-18 23:09

If you dismiss a Dialog, you must not show the same instance again. Either create a new one, or use hide() instead of dismiss(). When using hide() you still have to dismiss() it when no longer needed.

查看更多
甜甜的少女心
5楼-- · 2019-01-18 23:09

You can declare a ProgressDialog instance in your Activity and override onCreateDialog(int). Then instead of making a call to showDialog(int) you just assign your instance of ProgressDialog the call to onCreateDialog(int) where you will call dialog.show().

public class Home extends Activity {

ProgressDialog recordingDialog;

//where you want to call the dialog
recordingDialog = (ProgressDialog)onCreateDialog(RECORDING_DIALOG_KEY);


@Override
protected Dialog onCreateDialog(int id) {
    ProgressDialog dialog = new ProgressDialog(this);

    switch (id) {
    case RECORDING_DIALOG_KEY:
        ((ProgressDialog) dialog).setIndeterminate(true);
        dialog.setCancelable(false);
        ((ProgressDialog) dialog)
                .setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("Recording...");
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Stop",
                dialogStopListener);

        dialog.show();

        break;
    default:
        dialog = null;
    }
    return dialog;
}
}
查看更多
The star\"
6楼-- · 2019-01-18 23:11

I was looking for the same answer, however none of the answers here worked for me because I wanted to use the Activity's dialog methods like Activity.showDialog() so I didn't have to manage them myself and also so they would automatically be recreated on a rotation.

Like some of the other answers said, if dismiss() is called on a ProgressDialog it will stop spinning. Activity.onCreateDialog() saves a reference to the dialog, so once it is created it will not be recreated on the next Activity.showDialog() call. It will just call Activity.onPrepareDialog() and use the instance it already had. If it was dismissed then it won't spin when it is showed again.

You can force it to recreate itself if you call Activity.removeDialog() with the ProgressDialog's id instead of calling Dialog.dismiss(). This will dismiss it and also remove that reference so when Activity.onShowDialog() is called next it will use Activity.onCreateDialog() instead of only Activity.onPrepareDialog() and create a brand new (and spinning) instance.

Make sure if you have a ProgressDialog that can be dismissed or canceled you set a listener so you can call Activity.removeDialog(). Otherwise it will only be dismissed. You could also call it right before you call Activity.showDialog().

Hope that helps someone.

查看更多
登录 后发表回答