Custom DialogPreference disappears after orientation change

519 views Asked by At

First of all, sorry for my english.

I'm trying to make a custom 'day picker' dialog on my preference screen. I got the codes from Android Developer site and from Stackoverflow.

The dialog works 'well', it saves and loads the value (the layout contains a NumberPicker and a TextView). The problem is: the dialog disappears upon device rotation.

I have read several posts on this forum and other sites, but no luck. Can you help me ? I've spent hours so far to finding the issue, but I can't see how to solve it. Thanks.

The code:

package hu.test.android.demo.ui;

import hu.test.android.demo.AMdtApplication;
import hu.test.android.demo.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.NumberPicker;

public class DayPreference extends DialogPreference {

private NumberPicker picker = null;
private static int day;

public static int getDay () {
    return day;
}

public DayPreference(Context ctxt, AttributeSet attrs) {
    super(ctxt, attrs);

    setDialogLayoutResource(R.layout.day_preference);

    setPositiveButtonText (TestApplication.getContext().getResources().getString( R.string.save ));
    setNegativeButtonText (TestApplication.getContext().getResources().getString( R.string.cancel ));
}

@Override
protected void onBindDialogView(View v) {
    super.onBindDialogView (v);

    picker = (NumberPicker) v.findViewById (R.id.day_preference_number_picker);

    picker.setMinValue (1);
    picker.setMaxValue (30);
    picker.setValue (day);
}

@Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

    if (positiveResult) {
        if (callChangeListener (day)) {
            day = picker.getValue();
            persistInt (day);
        }

        setSummary(getSummary());
    }

}

@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
    return (a.getString(index));
}

@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {

    if (restoreValue) {
        if (defaultValue == null) {
            day = getPersistedInt (30);
        }
        else {
            day = getPersistedInt ((Integer) defaultValue);
        }
    }
    else {
        day = (Integer) defaultValue;
    }

    setSummary (getSummary());
}

@Override
public CharSequence getSummary() {
    return String.valueOf ( day ) + " " + TestApplication.getContext().getResources().getString( R.string.label_day );
}

// state save & restore
@Override
protected Parcelable onSaveInstanceState () {
    final Parcelable superState = super.onSaveInstanceState();

    if (isPersistent()) {
        return superState;
    }

    final SavedState myState = new SavedState (superState);
    myState.value = day;

    return myState;
}

@Override
protected void onRestoreInstanceState (Parcelable state) {

    if (state == null || !state.getClass().equals(SavedState.class)) {
        super.onRestoreInstanceState(state);

        return;
    }

    SavedState myState = (SavedState) state;
    super.onRestoreInstanceState(myState.getSuperState());

    // set the value to the picker
    picker.setValue (myState.value);
}

private static class SavedState extends BaseSavedState {
    public int value;

    public SavedState (Parcelable superState) {
        super(superState);
    }

    public SavedState (Parcel source) {
        super(source);
        value = source.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeInt(value);
    }

    // Standard creator object using an instance of this class
    @SuppressWarnings( "unused" )
    public static final Parcelable.Creator <SavedState> CREATOR = new Parcelable.Creator <SavedState> () {

        public SavedState createFromParcel(Parcel in) {
            return new SavedState(in);
        }

        public SavedState[] newArray (int size) {
            return new SavedState[size];
        }
    };
}

}

1

There are 1 answers

2
Sarthak Mittal On BEST ANSWER

UPDATE:

Please note that the below solution is just a quick hack(and is not a recommended approach), proper solution would be to persist values during onSaveInstanceState and then getting those values after your activity is recreated.

This post is a must read to gracefully handle configuration changes.

ORIGINAL ANSWER:

Maybe this will help, add this line inside the relevant activity tag in your manifest file:

android:configChanges="orientation|screenSize"

This will ensure that onCreate is not called when the screen rotates, you say to the android OS, hey i will be handling the rotation stuff myself for this activity you don't need to start over from onCreate.