Question

When I do the following:

SharedPreferences.Editor edit = getSharedPreferences(context,
                                     Context.MODE_PRIVATE).edit();
edit.putBoolean("boolean_1", true).putInt("int_1", 1);
edit.apply();

In what order will onSharedPreferencesChanged(String key, SharedPreferences sharedPreferences) be called? (key == "boolean_1") first, and then (key == "int_1"); or is it the other way around? Or is it neither of those scenarios, but rather another type of ordering, perhaps alpha/num sorting of keys, or by type + alpha?

Also, would the following do the exact same thing as the above code?

SharedPreferences.Editor edit = getSharedPreferences(context,
                                     Context.MODE_PRIVATE).edit();
edit.putBoolean("boolean_1", true)
edit.putInt("int_1", 1);
edit.apply();
Was it helpful?

Solution

You can see implementation at android source, line 399.

As I see, all changed keys are added into Map, so i can suggest tha it will be in alphabetical order.

Futher, onSharedPreferenceChanged will be called from end of Map to begin (line 450). So, in your example first will be called with (key == "int_1") and second (key == "boolean_1").

Also, there is no matter how you put changes - as in first or in the second code, all listeners will be notified after calling apply().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top