Mhm...
This is the code of SharedPreferences.Editor.commit() in 2.2.1
2825 if (hasListeners) {
2826 for (int i = keysModified.size() - 1; i >= 0;
i--) {
2827 final String key = keysModified.get(i);
2828 for (OnSharedPreferenceChangeListener
listener : listeners) {
2829 if (listener != null) {
2830
listener.onSharedPreferenceChanged(SharedPreferencesImpl.this, key);
2831 }
2832 }
2833 }
2834 }
whereas in 2.3.3 following code was refactored to a new method and is
executed at the "same" point
3020 private void notifyListeners(final MemoryCommitResult mcr) {
3021 if (mcr.listeners == null || mcr.keysModified ==
null ||
3022 mcr.keysModified.size() == 0) {
3023 return;
3024 }
3025 if (Looper.myLooper() == Looper.getMainLooper()) {
3026 for (int i = mcr.keysModified.size() - 1; i >=
0; i--) {
3027 final String key =
mcr.keysModified.get(i);
3028 for (OnSharedPreferenceChangeListener
listener : mcr.listeners) {
3029 if (listener != null) {
3030
listener.onSharedPreferenceChanged(SharedPreferencesImpl.this, key);
3031 }
3032 }
3033 }
3034 } else {
3035 // Run this function on the main thread.
3036 ActivityThread.sMainThreadHandler.post(new
Runnable() {
3037 public void run() {
3038 notifyListeners(mcr);
3039 }
3040 });
3041 }
3042 }
So yes, SharedPreferences.Editor.commit() was there since 1.0, but
between 2.2 and 2.3 the implementation changed to be able to handle
notifying listeners on the main thread. The difference between apply()
and commit() is that the former enqueues the writing to the file in a
separate thread.
On May 9, 11:57 am, Mark Murphy <[email protected]> wrote:
> On Mon, May 9, 2011 at 5:48 AM, laborg <[email protected]> wrote:
> > Thx! You gave me the hint to check the code and If my understanding is
> > right the implementation (editor.commit()) has changed between 2.2 and
> > 2.3 (2.3 runs in the main thread, 2.2 doesn't have this feature). It
> > would be interesting to see the API documentation for 2.2
>
> commit() has existed since Android 1.0. apply() is the new one.
> apply() uses a background thread; commit() never has. In fact, that is
> the reason apply() was added -- so saving SharedPreference changes
> could be done off the main application thread.
>
> --
> Mark Murphy (a Commons
> Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
>
> Android Training...At Your Office:http://commonsware.com/training
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en