如果我正确执行以下操作,任何人都可以看看。我有一个带有进度对话框的片段,我需要它在方向开关上工作。我目前这样做:
// I am using the compat libraries
import android.support.v4.app.DialogFragment;
public class ProgressDialogFragment extends DialogFragment {
private ProgressDialog mProgressDialog = null;
private int mMax = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, 0);
setRetainInstance(true);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setTitle("Title");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
mProgressDialog.setProgress(0);
mProgressDialog.setMax(mMax);
mProgressDialog.setCanceledOnTouchOutside(false);
return mProgressDialog;
}
// there seems to be a bug in the compat library - if I don't do the following - the dialog is not show after an orientation switch
@Override
public void onDestroyView() {
if (getDialog() != null && getRetainInstance())
getDialog().setDismissMessage(null);
super.onDestroyView();
}
public void setMax(int arg1) {
mProgressDialog.setMax(arg1);
mMax = arg1;
}
public void setProgress(int arg1) {
mProgressDialog.setProgress(arg1);
}
}
在我的活动中,我创建了这个 ProgressDialogFragment,并在需要显示对话框时调用 show()。我试图理解为什么在 onCreateDialog 方法中我不能简单地返回 mProgressDialog 如果它已经存在(我得到一个异常说“在添加内容之前必须调用requestFeature()”)。当然,片段的用途之一是在这些情况下重用资源 - 为什么我需要创建一个新对话框而不是使用已经存在的对话框?