I tried to find out how to safely assert that preferences aren’t stored anywhere. It seems that haven’t understood the caching mechanism yet and the docs don’t clarify it. This is what I did:
File prefsFile = new File("/data/data/"+context.getPackageName() + "/shared_prefs/"
+ context.getPackageName() + "_preferences.xml");
prefsFile.delete();
assertFalse(prefsFile.exists()); // success
// This assertion could fail - why?
assertEquals(0, context.getSharedPreferences(context.getPackageName()+"_preferences",
MODE_PRIVATE).getAll().size());
Although I deleted the prefs file, it is still possible that getAll().size() returns a non-zero value.
Could someone explain why?
Why?
Off the top of my head:
SharedPreferencesHashMapreturned bygetAll()to see if your data is there, or if it is some system-supplied initial valuesIf you want to clear out
SharedPreferences, do not try deleting the file. Either calledit().clear().commit()oredit().clear().apply()on yourSharedPreferencesobject.