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

有帮助吗?

解决方案

There is no difference in how you get and set SharedPreferences normally and from doing so in onResume. What you will need to do in addition to getting the most recent preferences, is update any objects you have in the Activity that use preference values. This will ensure your Activity is working with the most recent 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", "");
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top