Android:onSharedPreferenceChanged不会更改PreferenceScreen的摘要

时间:2016-04-23 06:05:54

标签: android android-preferences

我有一个子屏幕:

<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"已更新其摘要,但当我返回根首选项时,摘要不会更改。

当我重新启动首选项活动时,摘要会反映当前值。它初始化为:

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)

2 个答案:

答案 0 :(得分:3)

将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上有一个演示项目。您可以检查整个代码。或者您可以直接跳至this part of the demo app

答案 1 :(得分:1)

我注意到你正在倾听OnSharedPreferenceChangeListener,试着听/实施OnPreferenceChangeListener - 然后处理

    onPreferenceChange(Preference preference, Object newValue){
    ...
    if(preference.getKey().equals("pref_game_operation_plus")){
       boolean enabled = Boolean.parseBoolean(newValue);
       preference.setSummary(enabled);
    }
   ...
}