Java 重建PreferenceFragment时首选项的值错误
我正在我的首选项片段的onCreate方法中创建几个颜色选择器首选项(从首选项扩展而来) 颜色选择器描述当前激活的设计。因此,当用户选择一个新的设计(在相同的首选项片段中)时,所有颜色选择器都必须根据新的设计进行更改 为此,我将获得当前设计的新颜色值,并重建PreferenceFragmentJava 重建PreferenceFragment时首选项的值错误,java,android,oncreate,rebuild,preferencefragment,Java,Android,Oncreate,Rebuild,Preferencefragment,我正在我的首选项片段的onCreate方法中创建几个颜色选择器首选项(从首选项扩展而来) 颜色选择器描述当前激活的设计。因此,当用户选择一个新的设计(在相同的首选项片段中)时,所有颜色选择器都必须根据新的设计进行更改 为此,我将获得当前设计的新颜色值,并重建PreferenceFragment public static void RebuildSettings() { colorFieldList = GetNewColorFields(); if (mPrefsFragment != n
public static void RebuildSettings() {
colorFieldList = GetNewColorFields();
if (mPrefsFragment != null) {
mPrefsFragment.onDestroy();
mPrefsFragment.onCreate(null);
} else Log.i(Patterns.TAG, "mPrefsFragment = null");
}
在我的首选项片段的onCreate方法中,我然后从colorFieldList(使用新设计的颜色)重新创建颜色字段,如下所示:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
[...]
PreferenceCategory colorSettings = (PreferenceCategory) findPreference("prefCat_ColorSettings");
for (int i = 0; i < colorFieldList.size(); i++) {
AmbilWarnaPreference colorPicker = new AmbilWarnaPreference(getActivity(), null);
colorPicker.forceSetValue(colorFieldList.get(i).color);
colorPicker.setTitle(colorFieldList.get(i).name);
colorPicker.setSummary("Set color in " + colorFieldList.get(i).name);
colorPicker.setKey("colorField" + colorFieldList.get(i).index);
colorPicker.setOnPreferenceChangeListener(colorListener);
if (colorSettings != null)
colorSettings.addPreference(colorPicker);
}
[...]
}
getPreferenceManager().getSharedPreferences().edit().putInt("colorField" + colorFieldList.get(i).index, colorFieldList.get(i).color).apply();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
[...]
PreferenceCategory colorSettings=(PreferenceCategory)FindReference(“prefCat_colorSettings”);
对于(int i=0;i
现在,我可以更改设计,并且颜色字段将根据新设计进行良好更新,但仅当我尚未更改任何颜色选择器字段时。
字段会像预期的那样更改,但一旦我更改颜色字段/选择新颜色,此字段将永远保持我选择的颜色,尽管每次更改设计时我都会完全重建设置
我错过了什么?为什么只要没有值保存到SharedReferences(?)而不是之后,颜色就会更新
我猜一旦设置好,android总是从共享首选项中获取颜色选择器的值,但是我如何在OnCreate中覆盖它来描述所选设计中的颜色呢
非常感谢你的帮助!谢谢 我找到了答案:就像我假设的那样,问题是项目总是收到其特定键的默认值 要覆盖此行为,我必须在我的首选项片段的onCreate()方法中的编辑器上调用Apply()(我猜commit也会起作用),如下所示:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
[...]
PreferenceCategory colorSettings = (PreferenceCategory) findPreference("prefCat_ColorSettings");
for (int i = 0; i < colorFieldList.size(); i++) {
AmbilWarnaPreference colorPicker = new AmbilWarnaPreference(getActivity(), null);
colorPicker.forceSetValue(colorFieldList.get(i).color);
colorPicker.setTitle(colorFieldList.get(i).name);
colorPicker.setSummary("Set color in " + colorFieldList.get(i).name);
colorPicker.setKey("colorField" + colorFieldList.get(i).index);
colorPicker.setOnPreferenceChangeListener(colorListener);
if (colorSettings != null)
colorSettings.addPreference(colorPicker);
}
[...]
}
getPreferenceManager().getSharedPreferences().edit().putInt("colorField" + colorFieldList.get(i).index, colorFieldList.get(i).color).apply();
(我在上面发布的循环中这样做了,因此每个颜色首选项在创建时都会被指定给一个新颜色,并带有一个覆盖的首选项值)没有人知道吗?:-/