Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d532d8d8 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick Committed by Android Git Automerger
Browse files

am ca1db5ae: am dd644c17: Fallback to SharedPreferences.commit() when no apply() exists.

Merge commit 'ca1db5ae'

* commit 'ca1db5ae':
  Fallback to SharedPreferences$Editor.commit() when no apply() exists.
parents 49bbd662 ca1db5ae
Loading
Loading
Loading
Loading
+15 −3
Original line number Diff line number Diff line
@@ -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();
    }
+8 −1
Original line number Diff line number Diff line
@@ -1242,7 +1242,14 @@ public class Preference implements Comparable<Preference>, OnDependencyChangeLis

    private void tryCommit(SharedPreferences.Editor editor) {
        if (mPreferenceManager.shouldCommit()) {
            try {
                editor.apply();
            } catch (AbstractMethodError unused) {
                // The app injected its own pre-Gingerbread
                // SharedPreferences.Editor implementation without
                // an apply method.
                editor.commit();
            }
        }
    }
    
+20 −5
Original line number Diff line number Diff line
@@ -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();
            }
        }
    }
    
@@ -499,9 +508,15 @@ public class PreferenceManager {

    private void setNoCommit(boolean noCommit) {
        if (!noCommit && mEditor != null) {
            try {
                mEditor.apply();
            } catch (AbstractMethodError unused) {
                // The app injected its own pre-Gingerbread
                // SharedPreferences.Editor implementation without
                // an apply method.
                mEditor.commit();
            }
        }
        
        mNoCommit = noCommit;
    }