android - SharedPreferences - modifying multiple values - order of value change

StackOverflow https://stackoverflow.com/questions/23461455

  •  15-07-2023
  •  | 
  •  

Вопрос

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();
Это было полезно?

Решение

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().

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top