1

我使用以下代码按照新的“片段方式”创建了一个进度对话框:

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

如您所见,我得到了对对话框的引用并设置了它(取决于我的需要)。一切都像魅力一样,但是..如果我旋转设备,对话框会回到原始状态,而不是保留(在示例中:栏设置回不确定状态)我的新设置。我检查了我是否错误地创建和显示了一个新对话框,但事实并非如此。那么..我怎样才能在活动的娱乐中保持我的变化?

4

2 回答 2

1

你有没有尝试过

setRetainInstance(true)

由于 DialogFragment 扩展了基本 Fragment,我很确定它会起作用。

于 2013-06-11T01:09:08.180 回答
0

好的..这只是一种解决方法,我不建议这样做。尽管有displainer:我真的没有必要的时间来深入研究这个错误的逻辑,我使用了这个简单的解决方法。简而言之:每次我恢复对话框片段时,我都会使用它应该自动拥有的所有设置来更新它。这是一个例子:

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

}

无论如何:如果有人发现这种奇怪行为的原因,我将非常乐意正确修复我的代码。

于 2013-06-11T02:00:20.590 回答