PreferenceFragment.java 文件源码

java
阅读 31 收藏 0 点赞 0 评论 0

项目:AndroidPreferenceActivity 作者:
/**
 * Restores the default preferences, which are contained by a specific preference group.
 *
 * @param preferenceGroup
 *         The preference group, whose preferences should be restored, as an instance of the
 *         class {@link PreferenceGroup}. The preference group may not be null
 * @param sharedPreferences
 *         The shared preferences, which should be used to restore the preferences, as an
 *         instance of the type {@link SharedPreferences}. The shared preferences may not be
 *         null
 */
private void restoreDefaults(@NonNull final PreferenceGroup preferenceGroup,
                             @NonNull final SharedPreferences sharedPreferences) {
    for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
        Preference preference = preferenceGroup.getPreference(i);

        if (preference instanceof PreferenceGroup) {
            restoreDefaults((PreferenceGroup) preference, sharedPreferences);
        } else if (preference.getKey() != null && !preference.getKey().isEmpty()) {
            Object oldValue = sharedPreferences.getAll().get(preference.getKey());

            if (notifyOnRestoreDefaultValueRequested(preference, oldValue)) {
                sharedPreferences.edit().remove(preference.getKey()).apply();
                preferenceGroup.removePreference(preference);
                preferenceGroup.addPreference(preference);
                Object newValue = sharedPreferences.getAll().get(preference.getKey());
                notifyOnRestoredDefaultValue(preference, oldValue, newValue);
            } else {
                preferenceGroup.removePreference(preference);
                preferenceGroup.addPreference(preference);
            }

        }
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号