我onCreateDialog()
创建ProgressDialog
像这样:ProgressDialog停止动画在第二次运行
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实例,但动画已停止工作。
我试着重新设置onPrepareDialog()
不确定,但是这没有做任何事情。同样没有明显的方法调用将重置动画的对话框实例。
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);
}
编辑:但也许有一种方法来获得ProgressBar本身,并启动它的动画?所以我尝试以下后,我问这个问题:
@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回答,但琢磨之后,我不认为这是一个很好的解决了我的问题)
不需要调用'dismissDialog'方法 - 你可以直接调用'removeDialog',因为'如果显示对话框,它将会把它作为清理的一部分去除(来自JavaDoc) – Idolon 2011-04-29 09:05:22