Question

I'm trying to build my PreferenceActivity using fragments. I have a PreferenceFragment containing two CheckBoxPreferences:

<CheckBoxPreference
    android:key="pref_bfp_scale"
    android:title="@string/PREF_BFP_scale_title"
    android:defaultValue="false" />
<CheckBoxPreference
    android:key="pref_bfp_navy"
    android:title="@string/PREF_BFP_navy_title"
    android:defaultValue="true" />

My FragmentSettingsActivity:

public class FragmentSettingsActivity extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public static class BodyFatPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_body_fat);
            bindPrefSumtoValListener(findPreference("pref_bfp_scale"), "Boolean");
            bindPrefSumtoValListener(findPreference("pref_bfp_navy"), "Boolean");
        }
    }

    private static Preference.OnPreferenceChangeListener prefSumtoValListener = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
            String stringValue = value.toString();
            log("Pref changed - Value: " + stringValue);

            if (preference instanceof CheckBoxPreference) {
                Map<String, Integer> checkboxes = new HashMap<String, Integer>();

                checkboxes.put("pref_bfp_navy_unchecked", R.string.PREF_BFP_navy_summary_unchecked);
                checkboxes.put("pref_bfp_navy_checked", R.string.PREF_BFP_navy_summary_checked);
                checkboxes.put("pref_bfp_scale_unchecked", R.string.PREF_BFP_scale_summary_unchecked);
                checkboxes.put("pref_bfp_scale_checked", R.string.PREF_BFP_scale_summary_checked);

                log(Boolean.toString(((CheckBoxPreference) preference).isChecked()));
                log(String.format("? : %s_%s", preference.getKey(), ((CheckBoxPreference) preference).isChecked()? "checked" : "unchecked"));

                preference.setSummary(checkboxes.get(String.format("%s_%s", preference.getKey(), ((CheckBoxPreference) preference).isChecked()? "checked" : "unchecked")));
            }

            return true;

        }
    };

    private static void bindPrefSumtoValListener(Preference preference, String type) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(preference.getContext());

        preference.setOnPreferenceChangeListener(prefSumtoValListener);

        if (type.equals("Boolean")) {
            prefSumtoValListener.onPreferenceChange(preference, sharedPreferences.getBoolean(preference.getKey(), false));
        }
    }
}

The first call of onPreferenceChange sets the summary properly but every other call sets the contrary summary: If the Box is checked, the "unchecked" summary is set and vice versa.

Seems that isChecked() returns some sort of wrong value since value.toString() returns the correct state as a String.

log() prints:

log("Pref changed - Value: " + stringValue);
    //Output: Pref changed - Value: true
log(Boolean.toString(((CheckBoxPreference) preference).isChecked()));
    //Output: false
log(String.format("? : %s_%s", preference.getKey(), ((CheckBoxPreference) preference).isChecked()? "checked" : "unchecked"));
    //Output: ? : pref_bfp_navy_unchecked

Where is my mistake?

Was it helpful?

Solution

From the OnPreferenceChangeListener docs:

Returns
True to update the state of the Preference with the new value.

What can be gathered from this is that the value of the preference does not actually change until the listener returns true. In other words, the newValue parameter of onPreferenceChange() is what the value is going to be, but not what it currently is yet (because your log check is inside the listener, before true has been returned).

On a side note, I think the purpose of this is so that you are able to reject an invalid change to a preference. For instance, if the preference was a string, and it is desired that it only accepts strings from a certain array of accepted values, the dev can reject the change by returning false in onPreferenceChange().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top