Home » Android » android – delete all sharedPreferences for my app when user signs out

android – delete all sharedPreferences for my app when user signs out

Posted by: admin May 14, 2020 Leave a comment

Questions:

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?

How to&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..

Answer:

Simply put the following code, It works perfect for me…..

getApplicationContext().getSharedPreferences("CREDENTIALS", 0).edit().clear().commit();

Answer:

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();

Answer:

Use the code below to delete a preference key:

prefs.edit().remove("YOURTAG1").commit();
prefs.edit().remove("YOURTAG2").commit();

Answer:

Why not have a SharedPreference that keeps track of all associated files; then, in onPause or onStop, parse that value to SharedPreferences.Editor.clear().commit() each of them…then delete that last one?