am dd644c17
: Fallback to SharedPreferences.commit() when no apply() exists.
Merge commit 'dd644c179c1bf47d82d776d7f644e4fc1467159d' into gingerbread-plus-aosp * commit 'dd644c179c1bf47d82d776d7f644e4fc1467159d': Fallback to SharedPreferences$Editor.commit() when no apply() exists.
This commit is contained in:
@ -186,9 +186,21 @@ public interface SharedPreferences {
|
|||||||
* {@link #commit} will block until all async commits are
|
* {@link #commit} will block until all async commits are
|
||||||
* completed as well as the commit itself.
|
* completed as well as the commit itself.
|
||||||
*
|
*
|
||||||
* <p>If you call this from an {@link android.app.Activity},
|
* <p>As {@link SharedPreferences} instances are singletons within
|
||||||
* the base class will wait for any async commits to finish in
|
* a process, it's safe to replace any instance of {@link #commit} with
|
||||||
* its {@link android.app.Activity#onPause}.</p>
|
* {@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();
|
void apply();
|
||||||
}
|
}
|
||||||
|
@ -1195,7 +1195,14 @@ public class Preference implements Comparable<Preference>, OnDependencyChangeLis
|
|||||||
|
|
||||||
private void tryCommit(SharedPreferences.Editor editor) {
|
private void tryCommit(SharedPreferences.Editor editor) {
|
||||||
if (mPreferenceManager.shouldCommit()) {
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -443,7 +443,16 @@ public class PreferenceManager {
|
|||||||
pm.setSharedPreferencesMode(sharedPreferencesMode);
|
pm.setSharedPreferencesMode(sharedPreferencesMode);
|
||||||
pm.inflateFromResource(context, resId, null);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -478,15 +487,21 @@ public class PreferenceManager {
|
|||||||
boolean shouldCommit() {
|
boolean shouldCommit() {
|
||||||
return !mNoCommit;
|
return !mNoCommit;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setNoCommit(boolean noCommit) {
|
private void setNoCommit(boolean noCommit) {
|
||||||
if (!noCommit && mEditor != null) {
|
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;
|
mNoCommit = noCommit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the activity that shows the preferences. This is useful for doing
|
* Returns the activity that shows the preferences. This is useful for doing
|
||||||
* managed queries, but in most cases the use of {@link #getContext()} is
|
* managed queries, but in most cases the use of {@link #getContext()} is
|
||||||
|
Reference in New Issue
Block a user