简体   繁体   English

从横向/纵向旋转时,DatePickerDialog关闭

[英]DatePickerDialog closes when rotating from landscape/portrait

I'm having problem width DatePickerDialog, because when it's opened and I turn the screen (landscape/portrait) the dialog stops. 我遇到了宽度DatePickerDialog的问题,因为当它打开并旋转屏幕(横向/纵向)时,对话框将停止。
Now I'm trying to save the state of a dialog in a Bundle, but I don't really know how :/ 现在,我试图将对话框的状态保存在Bundle中,但我真的不知道如何://

The code: 编码:

public class ActivityNeki extends Activity {
    private int datY, datM, datD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        if(savedInstanceState == null){ setTheData(); writeTheData(); }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("izY", datY);
        outState.putInt("izM", datM);
        outState.putInt("izD", datD);

        super.onSaveInstanceState(outState);
    }
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        datY = savedInstanceState.getInt("izY");
        datM = savedInstanceState.getInt("izM");
        datD = savedInstanceState.getInt("izD");
        writeTheData();
    }

    public void onClickOpenDPD(View view) {    // the method that is caled from XML onClick
        DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override public void onDateSet(DatePicker view, int y, int m, int d) {
                datY = y; datM = m; datD = d;
                writeTheData();
            }
        }, datY, datM, datD);
        dpd.show();
    }

    public void setTheData(){
        Calendar c = Calendar.getInstance();
        datY = c.get(Calendar.YEAR);
        datM = c.get(Calendar.MONTH);
        datD = c.get(Calendar.DAY_OF_MONTH);
    }
    public void writeTheData(){
        // writes the data in a txtView
    }

}

So if anyon have any idea how to do this, I will apriceate it ! 因此,如果有人对如何执行此操作有任何想法,我将对其予以标价!

Thanks in advance :) 提前致谢 :)


The working solution: (Big tnx to Taig) 工作解决方案:(从 Big tnx到Taig)

public class ActivityNeki extends FragmentActivity {
    /**
    * the rest of the code
    * is the same...
    * I only edited this method:
    */
    public void onClickOpenDPD(View view) {    // the method that is caled from XML onClick
        class MyDialogFragment extends DialogFragment {
            @Override public void onDestroyView() {
                if (getDialog() != null && getRetainInstance()) getDialog().setDismissMessage(null);
                super.onDestroyView();
            }
            @Override public void onCreate(Bundle state) { super.onCreate(state);
                setRetainInstance(true);
            }
            @Override public Dialog onCreateDialog(Bundle state) {
                DatePickerDialog dpd = new DatePickerDialog( getActivity(), new DatePickerDialog.OnDateSetListener() {
                    @Override public void onDateSet(DatePicker view, int y, int m, int d) {
                        datY = y; datM = m; datD = d;
                        writeTheData();
                } }, datY, datM, datD);
                return dpd;
            }
        }
        newDF = new MyDialogFragment();
        newDF.show( getSupportFragmentManager(), null );
    }
}

I'd suggest using DialogFragments if that is an option for you. 我建议您使用DialogFragments。

class MyDialogFragment extends DialogFragment {
    @Override
    public void onCreate( Bundle state ) {
        super.onCreate( state );

        setRetainInstance( true );
    }

    @Override
    public Dialog onCreateDialog( Bundle state ) {
        return new DatePickerDialog( ... );
    }
}

In your activity you can then launch the dialog by calling new MyDialogFragment().show( getSupportFragmentManager(), null ) . 然后,在您的活动中,可以通过调用new MyDialogFragment().show( getSupportFragmentManager(), null )启动对话框。 The Dialog will reopen and you don't have to persist the current selection on orientation change by yourself! 对话框将重新打开,您不必自己继续更改方向的当前选择!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM