ProgressDialog从onCreateDialog创建的第二次运行停止动画动画、ProgressDialog、onCreateDialog

2023-09-05 11:06:31 作者:好听的神仙 落入凡间的神仙昵称

我创建了一个 ProgressDialog onCreateDialog()像这样:

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的,在其智慧(或严重缺乏)决定缓存()通过onCreateDialog创建的每个对话框。正因为如此,任何后续调用的ShowDialog(DIALOG_PROGRESS_ID)产生相同的ProgressDialog实例被使用,但动画已停止工作。

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.

我试图重新设置不确定的在prepareDialog(),但是,这并不做任何事情。这里同样没有明显的方法,在对话实例将重置动画调用。

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);
}

编辑:但也许有一种方式来获得的进度本身并启动动画?所以我尝试了以下后,我问这个问题:

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);

}

但它没有工作的或者的!

所以,没有人知道是否有一种方法重启动画在ProgressDialog? 如果没有,有没有办法,我可以强制每一个的ShowDialog()调用调用onCreateDialog()?(这第二个问题是由@TuomasR回答,但思考之后,我不认为这是一个很好的解决我的问题)

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)

推荐答案

哈!得到它...也挣扎与此有关。但调用:

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

removeDialog(DIALOG_PROGRESS_ID)

后,立即

dismissDialog(...)

从(presumed)将其删除对话框缓存的活动,并强制调用 onCreateDialog 。在 onCreateDialog 创建一个新的ProgressDialog和微调(至少对我来说)动画每次。

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).