Android SharedPreferences not saving

I would like to improve Chike's answer a bit.

Cause

The reason why commit() did not save to disk is obvious if we take a look at the implementation(unchanged since v4.0.1), which commit to memory first and then waits for disk writes to complete:

public boolean commit() {
    MemoryCommitResult mcr = commitToMemory();
    SharedPreferencesImpl.this.enqueueDiskWrite(
        mcr, null /* sync write on this thread okay */);
    try {
        mcr.writtenToDiskLatch.await();
    } catch (InterruptedException e) {
        return false;
    }
    notifyListeners(mcr);
    return mcr.writeToDiskResult;
}

The point is, no changes will be detected if we call putStringSet() with the same instance from getStringSet():

// Returns true if any changes were made
private MemoryCommitResult commitToMemory() {
    MemoryCommitResult mcr = new MemoryCommitResult();
    ...
            for (Map.Entry<String, Object> e : mModified.entrySet()) {
                String k = e.getKey();
                Object v = e.getValue();
                ...
                    if (mMap.containsKey(k)) {
                        Object existingValue = mMap.get(k);
                        if (existingValue != null && existingValue.equals(v)) {
                            continue;
                        }
                    }
                    mMap.put(k, v);
                }

                mcr.changesMade = true;
                ...
            }
            ...
        }
    }
    return mcr;
}

Since there is no change made, writeToFile() gladly skips disk writing and set a successful flag:

private void writeToFile(MemoryCommitResult mcr) {
    // Rename the current file so it may be used as a backup during the next read
    if (mFile.exists()) {
        if (!mcr.changesMade) {
            // If the file already exists, but no changes were
            // made to the underlying map, it's wasteful to
            // re-write the file.  Return as if we wrote it
            // out.
            mcr.setDiskWriteResult(true);
            return;
        }
        ...
    }
    ...
}

Solution

Please refer to Chike's answer, make a copy of the String set before saving it to the same SharedPreference key.


We just need to read the documentation more carefully

According to getStringSet

Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.

In fact it should have been noted in SharedPreferences.Editor not to send putStringSet a set that may modified afterwards. Make a copy of the set returned from getStringSet before modifying, and make a copy of your set before sending it to putStringSet.

SharedPreferences myPrefs = getSharedPreferences(myPrefName, MODE_PRIVATE);
HashSet<String> mySet = new HashSet<string>(myPrefs.getStringSet(mySetKey, new HashSet<string()));
....
SharedPreferences.Editor myEditor = myPrefs.edit();

Then one of

myEditor.putStringSet(mySetKey, new HashSet<String>(mySet));

or

myEditor.putStringSet(mySetKey, (Set<String>) mySet.clone());