0

I'm having problem width DatePickerDialog, because when it's opened and I turn the screen (landscape/portrait) the dialog stops.
Now I'm trying to save the state of a dialog in a Bundle, but I don't really know how :/

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)

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 );
    }
}
BRap
  • 529
  • 2
  • 10
  • 29

1 Answers1

3

I'd suggest using DialogFragments if that is an option for you.

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 ). The Dialog will reopen and you don't have to persist the current selection on orientation change by yourself!

Taig
  • 6,718
  • 4
  • 44
  • 65
  • tnx for the answer :) now it works at least without errors in LogCat.. But when I turn the screen, the Dialog still closes :/ any ideas ? Tnx again ! :) – BRap Feb 01 '15 at 22:39
  • Could you try to define the fragment as a static class out of the method scope (e.g. in an own file), please? Remove the onDataSet code for that, because it will stop working without the class reference. – Taig Feb 02 '15 at 03:54
  • Can't create a static class in another file... But I transferred the class in another file and it still close on rotation and if I create a static class in this activity it closes anyway :/ – BRap Feb 02 '15 at 15:52
  • 1
    Ouch, there is an issue with DialogFragments which I long forgot about and fixed somewhere deep in my tools library. You might wanna give http://stackoverflow.com/questions/12433397/android-dialogfragment-disappears-after-orientation-change a try! – Taig Feb 02 '15 at 17:27
  • 1
    Ok now that worked ! :D I will accept your answer, because it helped me a lot and with your help the datapicker now works !!! :) Tenx again :) – BRap Feb 02 '15 at 17:36