问题
I am trying an application where I am using Shared Preference. When I delete the preference file from data/data/com.your.package.name/shared_prefs/mySharedPref.xml manually using Android monitor, still the app is able to read the preference values.
I am assuming that some how the value is retained in main memory of the phone. Am I correct & what is the viable solution to clear shared preferences totally leaving no traces. But one thing I want to clear preference only if the file is wiped. For this I need to check presence of file, Any other approach rather than checking with File class ?
回答1:
I think this code must work
public static void clearAllPreference(Context context){
SharedPreferences prefs = context.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.commit();
}
回答2:
To remove specific values: SharedPreferences.Editor.remove()
followed by a commit()
To remove them all SharedPreferences.Editor.clear()
followed by a commit()
You use remove() to remove specific preferences, you use clear() to remove them all.
Checkout official documentation on SharedPreferences.Editor.
来源:https://stackoverflow.com/questions/35266600/android-shared-preference-still-visible-after-deleting-file