From c6f429007c2c402be71f69693cb1d2e6a1c8d0cf Mon Sep 17 00:00:00 2001 From: Narayan Kamath Date: Wed, 8 Jan 2014 11:58:32 +0000 Subject: [PATCH] Fix preference puts with "null" values. Null values were being written out as elements in the XML prefs file (as expected). This allowed the getFoo() functions to work correctly because they treated null values as missing mappings but containsKey would fail. bug: https://code.google.com/p/android/issues/detail?id=64563 Change-Id: I1f466d01db96bf26e208d4fed3a6f257228bea5d --- core/java/android/app/SharedPreferencesImpl.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/java/android/app/SharedPreferencesImpl.java b/core/java/android/app/SharedPreferencesImpl.java index 86fd7b977b1ad..a292ecbf4df0d 100644 --- a/core/java/android/app/SharedPreferencesImpl.java +++ b/core/java/android/app/SharedPreferencesImpl.java @@ -421,13 +421,15 @@ final class SharedPreferencesImpl implements SharedPreferences { for (Map.Entry e : mModified.entrySet()) { String k = e.getKey(); Object v = e.getValue(); - if (v == this) { // magic value for a removal mutation + // "this" is the magic value for a removal mutation. In addition, + // setting a value to "null" for a given key is specified to be + // equivalent to calling remove on that key. + if (v == this || v == null) { if (!mMap.containsKey(k)) { continue; } mMap.remove(k); } else { - boolean isSame = false; if (mMap.containsKey(k)) { Object existingValue = mMap.get(k); if (existingValue != null && existingValue.equals(v)) {