From dd644c179c1bf47d82d776d7f644e4fc1467159d Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 10 Oct 2010 10:58:47 -0700 Subject: Fallback to SharedPreferences$Editor.commit() when no apply() exists. Gingerbread widened the SharedPreferences.Editor interface, adding an apply() method. Most people don't implement this interface themselves, but a couple apps do. A few spots in the core framework take a SharedPreferences[.Editor] from apps, which might be a pre-Gingerbread implementation without an apply() method. This patch makes sure we never depend on the presence of an apply() method, falling back to commit() if apply() isn't available. Change-Id: I32693ac9227a60b694526a26a30234fb17a40581 --- core/java/android/content/SharedPreferences.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'core/java/android/content/SharedPreferences.java') diff --git a/core/java/android/content/SharedPreferences.java b/core/java/android/content/SharedPreferences.java index 1484204..3221afa 100644 --- a/core/java/android/content/SharedPreferences.java +++ b/core/java/android/content/SharedPreferences.java @@ -186,9 +186,21 @@ public interface SharedPreferences { * {@link #commit} will block until all async commits are * completed as well as the commit itself. * - *

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}.

+ *

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. + * + *

You don't need to worry about Android component + * lifecycles and their interaction with apply() + * writing to disk. The framework makes sure in-flight disk + * writes from apply() complete before switching + * states. + * + *

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 apply(), you can simply call + * {@link #commit} from apply(). */ void apply(); } -- cgit v1.1