开发者

How to update internal values of a PreferenceActivity when SharedPreferences change

I use a Preference in a PreferenceActivity to load default values: when this specific Preference is clicked, something like this happens:

private String mResetKeys = "key1,key2,key3";

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor prefs_editor = prefs.edit();
开发者_StackOverflowfor (String current_pref : mResetKeys.split(",")) {
    prefs_editor.remove(current_pref);
}
prefs_editor.commit();

But afterwards, the Preferences whose corresponding SharedPreference was reset still show the old value - it seems to be cached in the Preference. Only when I leave the PreferenceActivity and reopen it, the Preferences show the new values.

How can I update the PreferenceActivity programmatically?


I had a similar problem. This probably isn't the most correct fix but it worked for my purposes. Right after I did the commits, I called the Activity.recreate(); method.

The activity will restart (onDestroy()/onCreate()/etc) but for my purposes all I needed was a special handling on one preference. I listened for a certain preference with an OnPreferenceClickListener and made an alert dialog box with a kind of warning message and an option to change their mind. If they did want to change their mind, I did my commit of the new value to the preference activity and then called recreate() so that the checkbox preference would be updated.

However, I am also interested in a way to do this without recreating the activity...


Update preference value without reloading PreferenceActivity from http://liquidlabs.ca/2011/08/25/update-preference-value-without-reloading-preferenceactivity/

Here is how to update default shared preference value of target element (in this case EditTextPreference)

public class YourCustomPreference extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);
}

// some logic goes above, when you want to reset value and update EditTextPreference value
// For convenience, I am going to wrap two different task in different methods
private void resetPreferenceValue() {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
    SharedPreferences.Editor prefEditor = sharedPref.edit(); // Get preference in editor mode
    prefEditor.putString("your_edit_text_pref_key", "DEFAULT-VALUE"); // set your default value here (could be empty as well)
    prefEditor.commit(); // finally save changes

    // Now we have updated shared preference value, but in activity it still hold the old value
    this.resetElementValue();
} 

private void resetElementValue() {
    // First get reference to edit-text view elements
    EditTextPreference myPrefText = (EditTextPreference) super.findPreference("your_edit_text_pref_key");

    // Now, manually update it's value to default/empty
    myPrefText.setText("DEFAULT-VALUE"); // Now, if you click on the item, you'll see the value you've just set here
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜