3

In my app, preferences are organized by SharedPreferences. I need to perform reset to application defaults.

I know how to reset all values for particular SharedPreferences:

SharedPreferences prefs = getSharedPreferences(
      "name_of_the_preferences_file",
      MODE_PRIVATE
      );

SharedPreferences.Editor editor = prefs.edit();

editor.clear();
editor.commit();

But the problem is that only preferences for one particular preferences "name_of_the_preferences_file" is reset. Of course, all the rest preferences are not affected.

So, how to achieve full defauls reset?

It would be good to get names of all the preferences files, to reset it one-by-one.

But ideally, I want programmatically destroy all application data. This could be the best variant.

Dmitry Frank
  • 10,417
  • 10
  • 64
  • 114
  • I believe you'll find the answer here : http://stackoverflow.com/questions/3687315/deleting-shared-preferences – Philippe Girolami Aug 23 '12 at 08:52
  • Thanks, but there is only the same solution as I stated in my question, but to remove all the settings I need to have root access, isn't it? So unfortunately I can't find the answer there. – Dmitry Frank Aug 23 '12 at 09:38

1 Answers1

0

Original answer : see Deleting shared preferences

Edit : You stated "I need to perform reset to application defaults." and that link provides the solution: you didn't mention resetting all preferences of all apps. You cannot erase preferences from other apps from your own app because sharedpreferences are stored in a file and your app won't have write rights to directories that contain these files

Community
  • 1
  • 1
Philippe Girolami
  • 1,876
  • 1
  • 13
  • 15
  • 1
    Of course I do NOT want to reset prefs for all the apps. This would be very bad wish. Each app can have several prefs files (in my example, pref file name is "name_of_the_preferences_file"). And I want to get all the names of these files for my app. (then I will be able to reset settings in each file) – Dmitry Frank Aug 23 '12 at 10:56
  • You have to iterate over each shared pref & call clear() No other way – Philippe Girolami Aug 23 '12 at 14:01
  • 1
    But how can I get list with all the prefs that my app created? – Dmitry Frank Aug 23 '12 at 17:27
  • Oh that's your problem ? Why don't you use a single SharedPref. Instead of having Pref1, Pref2, Pref3, etc... with a bunch of keys you would have only one pref with jeys Pref1.*, Pref2.* – Philippe Girolami Aug 24 '12 at 08:13
  • 1
    If there's no way to get list, then maybe I'll have to do so, thanks. But this seems to be poor that I can't get list with all the file names.. – Dmitry Frank Aug 24 '12 at 09:42
  • You may be over-using preferences a little bit, maybe Sqlite would be better. I don't know... – Philippe Girolami Aug 24 '12 at 11:51