Search code examples
javaandroidrefreshpreferencespreferenceactivity

setSummary() is not refreshed immediately


I have the problem that the summary of a PreferenceScreen is not refreshed immediately.

I have a CheckBoxPreference in another PreferenceScreen. I'm catching the change of the CheckBox value in onSharedPreferenceChanged() and use setSummary() to change the summary of the screen. But when I go back to the main PreferenceScreen the changed summary is not displayed, just the old one.
I have to scroll down and up again until its displayed. So once its out of the view and come back in its refreshed.

How can I refresh it immediately?


Part of my PreferenceActivity:

public class AppPreferencesActivity extends android.preference.PreferenceActivity {

  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if (key.equals("pref1")) {
      this.findPreference("screen2").setSummary("mySummary");
    }
  }

Part of my preference.xml:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">    
    <PreferenceCategory android:title="Category Title">
        <PreferenceScreen android:key="screen2"
                          android:title="Screen Title">

            <CheckBoxPreference android:key="pref1"
                                android:title="Pref Title"                                
                                android:defaultValue="true" />
  </PreferenceCategory>
</PreferenceScreen>

Solution

  • This worked for me:

    BaseAdapter baseAdapter = (BaseAdapter) getPreferenceScreen().getRootAdapter();
    baseAdapter.notifyDataSetChanged();