diff options
author | Brad Fitzpatrick <bradfitz@android.com> | 2010-10-11 17:43:06 -0700 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2010-10-11 17:43:06 -0700 |
commit | d532d8d8c16c8b83a75310453d1f9f7197961bbf (patch) | |
tree | 45985faaeb3ef3a680855b8b7a7b42281105b596 /core | |
parent | 49bbd6625184a44cbdfc2014e55f4a0c519596f2 (diff) | |
parent | ca1db5ae68971779fd8af83c908128849f470ae0 (diff) | |
download | frameworks_base-d532d8d8c16c8b83a75310453d1f9f7197961bbf.zip frameworks_base-d532d8d8c16c8b83a75310453d1f9f7197961bbf.tar.gz frameworks_base-d532d8d8c16c8b83a75310453d1f9f7197961bbf.tar.bz2 |
am ca1db5ae: am dd644c17: Fallback to SharedPreferences.commit() when no apply() exists.
Merge commit 'ca1db5ae68971779fd8af83c908128849f470ae0'
* commit 'ca1db5ae68971779fd8af83c908128849f470ae0':
Fallback to SharedPreferences$Editor.commit() when no apply() exists.
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/content/SharedPreferences.java | 18 | ||||
-rw-r--r-- | core/java/android/preference/Preference.java | 9 | ||||
-rw-r--r-- | core/java/android/preference/PreferenceManager.java | 25 |
3 files changed, 43 insertions, 9 deletions
diff --git a/core/java/android/content/SharedPreferences.java b/core/java/android/content/SharedPreferences.java index c0788f5..4d9ee54 100644 --- a/core/java/android/content/SharedPreferences.java +++ b/core/java/android/content/SharedPreferences.java @@ -198,9 +198,21 @@ public interface SharedPreferences { * {@link #commit} will block until all async commits are * completed as well as the commit itself. * - * <p>If you call this from an {@link android.app.Activity}, - * the base class will wait for any async commits to finish in - * its {@link android.app.Activity#onPause}.</p> + * <p>As {@link SharedPreferences} instances are singletons within + * a process, it's safe to replace any instance of {@link #commit} with + * {@link #apply} if you were already ignoring the return value. + * + * <p>You don't need to worry about Android component + * lifecycles and their interaction with <code>apply()</code> + * writing to disk. The framework makes sure in-flight disk + * writes from <code>apply()</code> complete before switching + * states. + * + * <p class='note'>The SharedPreferences.Editor interface + * isn't expected to be implemented directly. However, if you + * previously did implement it and are now getting errors + * about missing <code>apply()</code>, you can simply call + * {@link #commit} from <code>apply()</code>. */ void apply(); } diff --git a/core/java/android/preference/Preference.java b/core/java/android/preference/Preference.java index 17b2e82..12b9f0c 100644 --- a/core/java/android/preference/Preference.java +++ b/core/java/android/preference/Preference.java @@ -1242,7 +1242,14 @@ public class Preference implements Comparable<Preference>, OnDependencyChangeLis private void tryCommit(SharedPreferences.Editor editor) { if (mPreferenceManager.shouldCommit()) { - editor.apply(); + try { + editor.apply(); + } catch (AbstractMethodError unused) { + // The app injected its own pre-Gingerbread + // SharedPreferences.Editor implementation without + // an apply method. + editor.commit(); + } } } diff --git a/core/java/android/preference/PreferenceManager.java b/core/java/android/preference/PreferenceManager.java index 42150ed..6562de9 100644 --- a/core/java/android/preference/PreferenceManager.java +++ b/core/java/android/preference/PreferenceManager.java @@ -461,7 +461,16 @@ public class PreferenceManager { pm.setSharedPreferencesMode(sharedPreferencesMode); pm.inflateFromResource(context, resId, null); - defaultValueSp.edit().putBoolean(KEY_HAS_SET_DEFAULT_VALUES, true).apply(); + SharedPreferences.Editor editor = + defaultValueSp.edit().putBoolean(KEY_HAS_SET_DEFAULT_VALUES, true); + try { + editor.apply(); + } catch (AbstractMethodError unused) { + // The app injected its own pre-Gingerbread + // SharedPreferences.Editor implementation without + // an apply method. + editor.commit(); + } } } @@ -496,15 +505,21 @@ public class PreferenceManager { boolean shouldCommit() { return !mNoCommit; } - + private void setNoCommit(boolean noCommit) { if (!noCommit && mEditor != null) { - mEditor.apply(); + try { + mEditor.apply(); + } catch (AbstractMethodError unused) { + // The app injected its own pre-Gingerbread + // SharedPreferences.Editor implementation without + // an apply method. + mEditor.commit(); + } } - mNoCommit = noCommit; } - + /** * Returns the activity that shows the preferences. This is useful for doing * managed queries, but in most cases the use of {@link #getContext()} is |