我使用以下代码在新的“片段方式”之后创建了一个progressdialog:
public class DialogUpdateTrackRecords extends DialogFragment {
public static DialogUpdateTrackRecords newInstance() {
DialogUpdateTrackRecords frag = new DialogUpdateTrackRecords();
return frag;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public ProgressDialog onCreateDialog(Bundle savedInstanceState) {
this.setCancelable(false);
ProgressDialog dialog= new ProgressDialog(getActivity());
dialog.setTitle("Caricamento tragitti");
dialog.setIndeterminate(true);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("Sending something");
return dialog;
}
}
我从这样的活动中展示它:
FragmentManager fm= getSupportFragmentManager();
uploadDialogFrament= (DialogFragment) getSupportFragmentManager().findFragmentByTag("sendDialog");
if(uploadDialogFrament!=null)
uploadDialogFrament.dismiss();
FragmentTransaction ft= fm.beginTransaction();
uploadDialogFrament= DialogUpdateTrackRecords.newInstance();
uploadDialogFrament.show(ft,"sendDialog");
fm.executePendingTransactions();
((ProgressDialog)uploadDialogFrament.getDialog()).setMax(trackRecordSize);
if( trackRecordSize > 1 )
((ProgressDialog)uploadDialogFrament.getDialog()).setIndeterminate(false);
正如您所看到的,我获得了对话框的引用,并设置了它(根据我的需要)。一切都像一个魅力,但..如果我旋转设备,对话框回到其原始状态,而不是保留(在示例中:栏设置回一个不确定状态)我的新设置。我检查了我是否正在创建并错误地显示一个新对话框,但事实并非如此。那么..我怎样才能保持对活动重建的改变?
答案 0 :(得分:1)
你试过吗
setRetainInstance(true)
由于DialogFragment扩展了基础片段,我非常确定它会起作用。
答案 1 :(得分:0)
好的..这只是一种解决方法,我不建议它。尽管存在置换器:我真的没有必要的时间深入研究这个bug的逻辑,我使用了这个简单的解决方法。简而言之:每次我恢复对话框片段时,我都会使用它应该自动拥有的所有设置进行更新。这是一个例子:
public class DialogUpdateTrackRecords extends DialogFragment {
private boolean indeterminate= true;
public static DialogUpdateTrackRecords newInstance() {
DialogUpdateTrackRecords frag = new DialogUpdateTrackRecords();
return frag;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public void onResume() {
super.onResume();
((ProgressDialog)getDialog()).setIndeterminate(indeterminate);
}
@Override
public ProgressDialog onCreateDialog(Bundle savedInstanceState) {
this.setCancelable(false);
ProgressDialog dialog= new ProgressDialog(getActivity());
dialog.setTitle("Caricamento tragitti");
dialog.setIndeterminate(true);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("Invio delle statistiche completate a ISF Modena in corso");
return dialog;
}
public void setIndeterminate(){
((ProgressDialog)getDialog()).setIndeterminate(false);
indeterminate= false;
}
/*
[italian soh]
http://stackoverflow.com/questions/12433397/android-dialogfragment-disappears-after-orientation-change
thanks, google. Thanks.
[/italian soh]
*/
@Override
public void onDestroyView() {
if (getDialog() != null && getRetainInstance())
getDialog().setDismissMessage(null);
super.onDestroyView();
}
}
无论如何:如果有人发现这种奇怪行为的原因,我会非常乐意正确修复我的代码。