Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete all sharedPreferences for my app when user signs out

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?

like image 452
Cote Mounyo Avatar asked Aug 21 '13 04:08

Cote Mounyo


2 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..

like image 141
kalyan pvs Avatar answered Sep 23 '22 16:09

kalyan pvs


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());
like image 45
Dankyi Anno Kwaku Avatar answered Sep 22 '22 16:09

Dankyi Anno Kwaku