I have an android app with a number of activities, and a number of specific preference files that are created. When a user sign out of my app, I want to delete absolutely all shared preferences. Understand that I cannot use context.getSharedPreferences("A_PREFS_FILE", 0).edit().clear().commit() as that would only clear one specific file. I want to clear all the preference files associated with my app. Is there a way to do that?
8 Answers
By this way you can delete all the files at ones.. by clear() it will erase the data file still exist..
File sharedPreferenceFile = new File("/data/data/"+ getPackageName()+ "/shared_prefs/");
File[] listFiles = sharedPreferenceFile.listFiles();
for (File file : listFiles) {
file.delete();
}
Here it will returns the all the list of files then you can easily delete..
3 Comments
Yoraco Gonzales
This is definitivly the right way to delete all shared preferences files. But consider to use
context.getFilesDir().getPath() instead of hard coding the path "/data/data/"Jemshit
@YoracoGonzales
context.getFilesDir().getPath() gives me /data/user/0/{applicationId}/files which is not equal to /data/data/Jemshit
context.filesDir.parentFile.absolutePath + File.separator + "shared_prefs" did work. But who knows maybe it will not work on Samsung ¯_(ツ)_/¯Simply put the following code, It works perfect for me.....
getApplicationContext().getSharedPreferences("CREDENTIALS", 0).edit().clear().commit();
1 Comment
Félix Maroy
Consider using apply() instead; commit writes its data to persistent storage immediately, whereas apply will handle it in the background.
getApplicationContext().getSharedPreferences("PREF_NAME", 0).edit().clear().apply();First you have to clear then next call commit
Try it:
SharedPreference.Editor pref = context.getSharedPreferences("A_PREFS_FILE", 0).edit();
pref.clear();
pref.commit();
2 Comments
Sharon Joshi
what is A_PREFS_FILE
Pratik Butani
The question asked for
A_PREFS_FILE so, You can write your preference name.SharedPreferences directory path : Environment.getDataDirectory() + "/data/" + ActivateAccountActivity.this.getPackageName() + "/shared_prefs"
Use "for" loop to delete all the files, like so :
File sprefs_directory = new File(Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/shared_prefs");
File[] files = filesirectory.listFiles();
for(File file : files) {
file.delete();
}
Comments
This works
public static void deleteAllDataInSharedPreference(Context context){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
context = null;
}
deleteAllDataInSharedPreference(getApplicationContext());
clear()is a non-static method, and thereforeSharedPreferences.Editor.clear()would not work. All the other examples there are doing exactly what I specifically said I don't want to do. Also, following would be a null pointer:Editor defaultPrefsPut; defaultPrefsPut.clear(); defaultPrefsPut.commit();