Reload SharedPreferences on resume? (or how to update / reload activity)

How can I reload SharedPreferences when I return from one activity to another? If I return, the user may have changed the settings. Is it possible to reload SharedPreferences or do I need to refresh / reload activity. And if, then how?

+3
source share
1 answer

There is no difference in how you get and install SharedPreferencesas usual, and from that onResume. What you will need to do in addition to the last settings is updating any objects that you have in Activitythat use preference values. This ensures that yours Activitywill work with the latest values.

A simple example:

protected void onResume() {
    super.onResume();
        getPrefs();

    //...Now update your objects with preference values         
}

private void getPrefs() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    String myPref = prefs.getString("myPref", "");
}
+3
source