ListPreference default value not showing up

index = listPreference.findIndexOfValue(listPreference.value)
listPreference.setValueIndex(index)

I found that sometime I need to clear application data. Uninstall and reinstall the app. After that, everything works as expected.


I found that I have to call PreferenceManager.setDefaultValues() in my Preferences Activity in order for my default value to show up initially.

public class PreferencesActivity extends PreferenceActivity {

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

        // This static call will reset default values only on the first ever read
        PreferenceManager.setDefaultValues(getBaseContext(), R.xml.settings, false);

        addPreferencesFromResource(R.xml.settings);
    }
}