自定义首选项isPersistent()

时间:2016-07-30 19:33:34

标签: android android-preferences android-savedstate android-number-picker

我使用android's brief guide和一些谷歌搜索来编写自己的号码选择器偏好。我的问题是关于onSaveInstanceState()方法。在谷歌的教程中,建议我们使用方法isPersistent()来确定首选项是否持久,如果是,则只返回超级域名。我没有这样做,因为在这种情况下,如果我将数字选择器刷到一个新数字,然后旋转屏幕,旋转的版本将返回到持久值。如果我删除这个条件,那么一切都很好。但是,检查其他首选项的源代码,如edittextpreference,这个条件存在,即使我将值更改为未保存的,然后旋转屏幕,状态也会保存。有人可以解释一下吗? 这是我的代码:

public class NumberPreference extends DialogPreference {
    private final static int DEFAULT_VALUE=R.integer.timer_def;
    private final static int DEFAULT_MIN_VALUE=R.integer.timer_min_def;
    private final static int DEFAULT_MAX_VALUE=R.integer.timer_max_def;
    private final int min;
    private final int max;
    private final String time;

    private int timer;
    private NumberPicker numberPicker;


    public NumberPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDialogLayoutResource(R.layout.number_preference);
        setNegativeButtonText("Cancel");
        setPositiveButtonText("OK");
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.number_preference, 0, 0);
        try{
            min=a.getInteger(R.styleable.number_preference_min, DEFAULT_MIN_VALUE);
            max=a.getInteger(R.styleable.number_preference_max, DEFAULT_MAX_VALUE);
            time=a.getString(R.styleable.number_preference_time);
        }finally{
            a.recycle();
        }
        setDialogIcon(null);
    }

    public void setSummary() {
        super.setSummary("Every "+getTimer()+' '+time);
    }

    @Override
    protected View onCreateView(ViewGroup parent) {
        setSummary();
        return super.onCreateView(parent);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            int number = numberPicker.getValue();
            if (callChangeListener(number)){
                timer=number;
                persistInt(timer);
                setSummary();
            }
        }
    }

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

    @Override
    protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
        if (restorePersistedValue) {
            timer = getPersistedInt(DEFAULT_VALUE);
        }
        else{
            timer =(Integer) defaultValue;
            persistInt(timer);
        }
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        numberPicker=(NumberPicker) view.findViewById(R.id.numpref_picker);
        numberPicker.setMinValue(min);
        numberPicker.setMaxValue(max);
        numberPicker.setValue(timer);

    }

    public int getTimer() {
        return getPersistedInt(DEFAULT_VALUE);
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        final Parcelable superState = super.onSaveInstanceState();
        if (isPersistent()) {
            return superState;
        }
        final SavedState myState=new SavedState(superState);
        if (numberPicker!= null) myState.value=numberPicker.getValue();
        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());
        if (numberPicker!=null)numberPicker.setValue(myState.value);
    }

    private static class SavedState extends BaseSavedState {
        // field that holds the setting's value
        int value;

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

        public SavedState(Parcel source) {
            super(source);
            // Get the current preference's value
            value = source.readInt();
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            // Write the preference's value
            dest.writeInt(value);
        }

        // Standard creator object using an instance of this class
        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 个答案:

答案 0 :(得分:0)

未经测试,会假设您可能需要更改

private final String time;

private String time;

并将String.valueOf(time)作为参数传递给.setSummary()方法。当然,.onCreateView()中的出现需要传递从首选项中读取的值 - 或者如果没有返回任何内容则将默认值作为回退。尝试使更简单,更复杂。