解决dialog在横竖屏切换时消失

以AlertDialog为例

   一、将AlertDialog视图封装在dialogfragment(fragmentdeAlertDialog子类)实例中

 

    package com.example.t;

    import android.app.AlertDialog;            
    import android.app.Dialog;
    import android.os.Bundle;
    import android.widget.DatePicker;

    public class DialogPickerFragment extends android.support.v4.app.DialogFragment {
       @Override
       public Dialog onCreateDialog(Bundle savedInstanceState) {
            DatePicker dp = new DatePicker(getActivity());
            return new AlertDialog.Builder(getActivity())
            .setView(dp)
            .setTitle("Dialog")
            .setPositiveButton("OK", null)
            .create();
         }
    }

 

  二、在activity中调用

    public void dialogpicker(View v) {
          FragmentManager fm = getSupportFragmentManager();
          DialogPickerFragment dialog = new DialogPickerFragment();
          dialog.show(fm, "date");
    }

 

你可能感兴趣的:(dialog,横竖屏切换)