CheckBoxPreference和TwoStatePreference都没有重写setDefaultValue(Object),所以实际上是直接调用Preference.setDefaultValue(Object):
/**
* Sets the default value for this Preference, which will be set either if
* persistence is off or persistence is on and the preference is not found
* in the persistent storage.
*
* @param defaultValue The default value.
*/
public void setDefaultValue(Object defaultValue) {
mDefaultValue = defaultValue;
}
private void dispatchSetInitialValue() {
// By now, we know if we are persistent.
final boolean shouldPersist = shouldPersist();
if (!shouldPersist || !getSharedPreferences().contains(mKey)) {
if (mDefaultValue != null) {
onSetInitialValue(false, mDefaultValue);
}
} else {
onSetInitialValue(true, null);
}
}
/**
* Implement this to set the initial value of the Preference.
*
* If restorePersistedValue is true, you should restore the
* Preference value from the {@link android.content.SharedPreferences}. If
* restorePersistedValue is false, you should set the Preference
* value to defaultValue that is given (and possibly store to SharedPreferences
* if {@link #shouldPersist()} is true).
*
* This may not always be called. One example is if it should not persist
* but there is no default value given.
*
* @param restorePersistedValue True to restore the persisted value;
* false to use the given defaultValue.
* @param defaultValue The default value for this Preference. Only use this
* if restorePersistedValue is false.
*/
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
setChecked(restoreValue ? getPersistedBoolean(mChecked)
: (Boolean) defaultValue);
}
/**
* Sets the checked state and saves it to the {@link SharedPreferences}.
*
* @param checked The checked state.
*/
public void setChecked(boolean checked) {
// Always persist/notify the first time; don't assume the field's default of false.
final boolean changed = mChecked != checked;
if (changed || !mCheckedSet) {
mChecked = checked;
mCheckedSet = true;
persistBoolean(checked);
if (changed) {
notifyDependencyChange(shouldDisableDependents());
notifyChanged();
}
}
}
/**
* Called when this Preference has been attached to a Preference hierarchy.
* Make sure to call the super implementation.
*
* @param preferenceManager The PreferenceManager of the hierarchy.
*/
protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
mPreferenceManager = preferenceManager;
mId = preferenceManager.getNextId();
dispatchSetInitialValue();
}
/**
* Adds a {@link Preference} at the correct position based on the
* preference's order.
*
* @param preference The preference to add.
* @return Whether the preference is now in this group.
*/
public boolean addPreference(Preference preference) {
if (mPreferenceList.contains(preference)) {
// Exists
return true;
}
if (preference.getOrder() == Preference.DEFAULT_ORDER) {
if (mOrderingAsAdded) {
preference.setOrder(mCurrentPreferenceOrder++);
}
if (preference instanceof PreferenceGroup) {
// TODO: fix (method is called tail recursively when inflating,
// so we won't end up properly passing this flag down to children
((PreferenceGroup)preference).setOrderingAsAdded(mOrderingAsAdded);
}
}
if (!onPrepareAddPreference(preference)) {
return false;
}
synchronized(this) {
int insertionIndex = Collections.binarySearch(mPreferenceList, preference);
if (insertionIndex < 0) {
insertionIndex = insertionIndex * -1 - 1;
}
mPreferenceList.add(insertionIndex, preference);
}
preference.onAttachedToHierarchy(getPreferenceManager());
if (mAttachedToActivity) {
preference.onAttachedToActivity();
}
notifyHierarchyChanged();
return true;
}