Search code examples
androidpreferenceactivity

PreferenceActivity Refresh Problem


I have PreferemceActivity inflated from xml:

<PreferenceScreen
            android:title="Appearence"
            android:key="AppearencePref" >
            ......
            <PreferenceCategory
                android:title="Show Contact Photos">
                <CheckBoxPreference 
                    android:title="Show Contact Photos" 
                    android:summary="@string/show_contact_photos_preference"
                    android:defaultValue="true"
                    android:key="ShowContactPhotosCheckBoxPref_Appendix" />
            </PreferenceCategory>
       ........
</PreferenceScreen>

.......

<PreferenceScreen
            android:title="Contact Options"
            android:key="ContactOtionsPref">
            <PreferenceCategory 
                android:title="Show Contact Photos">
                <CheckBoxPreference 
                    android:title="Show Contact Photos"
                    android:defaultValue="true"
                    android:key="ShowContactPhotosCheckBoxPref" />
            </PreferenceCategory>
......            
</PreferenceScreen>

One of the preferences(checkbox) change state of other checkbox:

if("ShowContactPhotosCheckBoxPref_Appendix".equals(key)){
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
            boolean isChecked = prefs.getBoolean("ShowContactPhotosCheckBoxPref_Appendix", false);
            Editor editor = PreferenceManager.getDefaultSharedPreferences(mContext).edit();
            editor.putBoolean("ShowContactPhotosCheckBoxPref", isChecked);
            editor.commit();            
        }

But when I go to screen with ShowContactPhotosCheckBoxPref it still hold previous preference value... So if I click on ShowContactPhotosCheckBoxPref_Appendix - his state is now unchecked and then go to screen with ShowContactPhotosCheckBoxPref - his state still checked, but value in SharedPreferences is false...

How can I tell PreferenceActivity to refresh its value?


Solution

  • Solution is in ApiDemos AdvancedPreferences.java:

    private CheckBoxPreference mCheckBoxPreference;
    
    mCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(
            KEY_ADVANCED_CHECKBOX_PREFERENCE);
    
    if (mCheckBoxPreference != null) {
        mCheckBoxPreference.setChecked(!mCheckBoxPreference.isChecked());
    }