3

I am using getPreferences() method in different activities of my app. That means, Android create different files, one for each activity in which I am using that method.

I would like to clear all those files. Is there any way? Now, I delete every single file in the shared_prefs directory of the app, but I would like a more "safety" solution.

File folder = new File(getFilesDir() + File.separator + "shared_prefs" + File.separator);
                if (folder.exists() && folder.isDirectory()) {
                    String[] children = folder.list();
                    for (int i = 0; i < children.length; i++)
                        new File(folder, children[i]).delete();
                }
Daniele Vitali
  • 3,848
  • 8
  • 48
  • 71
  • 1
    possible duplicate of [Deleting shared preferences](http://stackoverflow.com/questions/3687315/deleting-shared-preferences) – RvdK Jun 18 '14 at 11:54
  • do you want to clear data from preference file?? – Krupa Patel Jun 18 '14 at 11:56
  • context.getSharedPreferences("YOUR_PREFS", 0).edit().clear().commit(); //remove all your prefs – Pragnesh Ghoda シ Jun 18 '14 at 11:59
  • @RvdK In that way, I gotta check the name of the preference files and clear all of them, one by one. Is there any way to reset all the preferences in one shot? – Daniele Vitali Jun 18 '14 at 12:02
  • @KrupaPatel clear data from all preference files, not just one. – Daniele Vitali Jun 18 '14 at 12:04
  • I don't see any difference with clear each preference files as opposed to deleting each file individually. Furthermore deleting a file which you don't have created manually will likely create undefined behavior. E.g. what happens when your app is open and you delete such files. – RvdK Jun 18 '14 at 12:09
  • 1
    This looks like a very good solution to me. Nothing wrong with it. The last `+ File.separator` is not needed. Nothing will go wrong. The app behaves as if it is newly installed. – greenapps Jun 18 '14 at 17:49

0 Answers0