Android: onSharedPreferenceChanged 不改变 PreferenceScreen 的摘要
Android: onSharedPreferenceChanged does not change a summary of PreferenceScreen
我有一个子屏幕:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Hra">
<PreferenceScreen
android:key="pref_game_plus_category"
android:title="@string/operation_plus"
android:persistent="false">
<CheckBoxPreference
android:key="pref_game_operation_plus"
android:title="@string/pref_title_operation_plus"
android:defaultValue="true"
/>
我在
中实例化首选项
public class GamePreferenceActivity extends PreferenceActivity
implements SharedPreferences.OnSharedPreferenceChangeListener {
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
log.debug("onSharedPreferenceChanged(" + key + ")");
PreferenceScreen preferenceScreen = getPreferenceScreen();
...
case "pref_game_operation_plus":
preferenceScreenHelper.setScreenSummary("plus", preferenceScreen, sharedPreferences);
这里我检测复选框的状态并设置父屏幕字幕:
public void setScreenSummary(String key, PreferenceScreen preferenceScreen, SharedPreferences sharedPreferences) {
boolean value = sharedPreferences.getBoolean("pref_game_operation_" + key, true);
Preference preference = preferenceScreen.findPreference("pref_game_" + key + "_category");
preference.setSummary(value ? R.string.pref_operation_enabled : R.string.pref_operation_disabled);
当我调试此代码时,"pref_game_plus_category"
更新了它的摘要,但是当我 return 到 root 首选项时,摘要并没有改变。
PS 当我重新启动首选项时 activity 摘要反映了当前值。它初始化为:
public void setScreenSummary(String key, Preference preference, SharedPreferences sharedPreferences) {
boolean value = sharedPreferences.getBoolean("pref_game_operation_" + key, true);
preference.setSummary(value ? R.string.pref_operation_enabled : R.string.pref_operation_disabled);
}
更新:摘要是第一次更新,但后续更改(即使是不同的屏幕)都没有效果。奇怪的是,我可以两次看到此首选项的条目日志,但调试器只停止一次。我必须深入研究 Android 来源。
04-23 08:36:46.471 5503-5503/lelisoft.com.lelimath D/l.c.l.a.GamePreferenceActivity: onSharedPreferenceChanged(pref_game_divide_category)
04-23 08:36:48.004 5503-5503/lelisoft.com.lelimath D/l.c.l.a.GamePreferenceActivity: onSharedPreferenceChanged(pref_game_operation_divide)
04-23 08:36:48.006 5503-5503/lelisoft.com.lelimath D/l.c.l.h.PreferenceHelper: pokus
04-23 08:36:48.007 5503-5503/lelisoft.com.lelimath D/l.c.l.a.GamePreferenceActivity: onSharedPreferenceChanged(pref_game_operation_divide)
我注意到你在听 OnSharedPreferenceChangeListener
,试着听 for/implement OnPreferenceChangeListener
-
然后处理
onPreferenceChange(Preference preference, Object newValue){
...
if(preference.getKey().equals("pref_game_operation_plus")){
boolean enabled = Boolean.parseBoolean(newValue);
preference.setSummary(enabled);
}
...
}
将您的 setScreenSummary 函数更改为以下内容:
public void setScreenSummary(String key, PreferenceScreen preferenceScreen, SharedPreferences sharedPreferences) {
boolean value = sharedPreferences.getBoolean("pref_game_operation_" + key, true);
//instead of getting a reference to preference typecast it to PreferenceScreen
PreferenceScreen preference = (PreferenceScreen) findPreference("pref_game_" + key + "_category");
//now set the summary
preference.setSummary(value ? R.string.pref_operation_enabled : R.string.pref_operation_disabled);
//here comes the important part
//get the listView adapter and call notifyDataSetChanged
//This is necessary to reflect change after coming back from sub-pref screen
((BaseAdapter)getPreferenceScreen().getRootAdapter()).notifyDataSetChanged();
}
我在gitlab上有一个演示项目PreferenceScreenDemo. You can check the whole code. Or you can directly skip to this part of the demo app
我有一个子屏幕:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Hra">
<PreferenceScreen
android:key="pref_game_plus_category"
android:title="@string/operation_plus"
android:persistent="false">
<CheckBoxPreference
android:key="pref_game_operation_plus"
android:title="@string/pref_title_operation_plus"
android:defaultValue="true"
/>
我在
中实例化首选项public class GamePreferenceActivity extends PreferenceActivity
implements SharedPreferences.OnSharedPreferenceChangeListener {
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
log.debug("onSharedPreferenceChanged(" + key + ")");
PreferenceScreen preferenceScreen = getPreferenceScreen();
...
case "pref_game_operation_plus":
preferenceScreenHelper.setScreenSummary("plus", preferenceScreen, sharedPreferences);
这里我检测复选框的状态并设置父屏幕字幕:
public void setScreenSummary(String key, PreferenceScreen preferenceScreen, SharedPreferences sharedPreferences) {
boolean value = sharedPreferences.getBoolean("pref_game_operation_" + key, true);
Preference preference = preferenceScreen.findPreference("pref_game_" + key + "_category");
preference.setSummary(value ? R.string.pref_operation_enabled : R.string.pref_operation_disabled);
当我调试此代码时,"pref_game_plus_category"
更新了它的摘要,但是当我 return 到 root 首选项时,摘要并没有改变。
PS 当我重新启动首选项时 activity 摘要反映了当前值。它初始化为:
public void setScreenSummary(String key, Preference preference, SharedPreferences sharedPreferences) {
boolean value = sharedPreferences.getBoolean("pref_game_operation_" + key, true);
preference.setSummary(value ? R.string.pref_operation_enabled : R.string.pref_operation_disabled);
}
更新:摘要是第一次更新,但后续更改(即使是不同的屏幕)都没有效果。奇怪的是,我可以两次看到此首选项的条目日志,但调试器只停止一次。我必须深入研究 Android 来源。
04-23 08:36:46.471 5503-5503/lelisoft.com.lelimath D/l.c.l.a.GamePreferenceActivity: onSharedPreferenceChanged(pref_game_divide_category)
04-23 08:36:48.004 5503-5503/lelisoft.com.lelimath D/l.c.l.a.GamePreferenceActivity: onSharedPreferenceChanged(pref_game_operation_divide)
04-23 08:36:48.006 5503-5503/lelisoft.com.lelimath D/l.c.l.h.PreferenceHelper: pokus
04-23 08:36:48.007 5503-5503/lelisoft.com.lelimath D/l.c.l.a.GamePreferenceActivity: onSharedPreferenceChanged(pref_game_operation_divide)
我注意到你在听 OnSharedPreferenceChangeListener
,试着听 for/implement OnPreferenceChangeListener
-
然后处理
onPreferenceChange(Preference preference, Object newValue){
...
if(preference.getKey().equals("pref_game_operation_plus")){
boolean enabled = Boolean.parseBoolean(newValue);
preference.setSummary(enabled);
}
...
}
将您的 setScreenSummary 函数更改为以下内容:
public void setScreenSummary(String key, PreferenceScreen preferenceScreen, SharedPreferences sharedPreferences) {
boolean value = sharedPreferences.getBoolean("pref_game_operation_" + key, true);
//instead of getting a reference to preference typecast it to PreferenceScreen
PreferenceScreen preference = (PreferenceScreen) findPreference("pref_game_" + key + "_category");
//now set the summary
preference.setSummary(value ? R.string.pref_operation_enabled : R.string.pref_operation_disabled);
//here comes the important part
//get the listView adapter and call notifyDataSetChanged
//This is necessary to reflect change after coming back from sub-pref screen
((BaseAdapter)getPreferenceScreen().getRootAdapter()).notifyDataSetChanged();
}
我在gitlab上有一个演示项目PreferenceScreenDemo. You can check the whole code. Or you can directly skip to this part of the demo app