0

I'm using this method to get SharedPreferences that are programatically set:

public static SharedPreferences getPrefs(final Context context) {
    return context.getSharedPreferences(context.getPackageName()
            + "_preferences", Context.MODE_WORLD_READABLE);
}

I'm also using prefs.xml in my PreferenceActivity like that:

addPreferencesFromResource(R.xml.prefs);

I would like to store all of the apps preferences in the same place (preferably: context.getPackageName() + "_preferences") ), how do I do that?

syntagma
  • 23,346
  • 16
  • 78
  • 134
  • Why do you want to do that ? Also I think `context.getSharedPreferences(context.getPackageName() + "_preferences", Context.MODE_WORLD_READABLE);` will return the default shared prefs in an undocumented manner – Mr_and_Mrs_D Dec 11 '13 at 22:53
  • I am setting a listener and would like to compare a value from the first set of shared preferences and from the second set (i.e.. based on `prefs.xml`). Should I just store all of my preferences in the default shared prefs? – syntagma Dec 12 '13 at 06:12

1 Answers1

1

The name you chose for your shared preferences file is the name of the default shared preferences file - and this will end up being bad. The doc for preference fragment says

These preferences will automatically save to SharedPreferences as the user interacts with them. To retrieve an instance of SharedPreferences that the preference hierarchy in this activity will use, call getDefaultSharedPreferences(android.content.Context) with a context in the same package as this activity.

The xml you load the preferences from is not related to the xml they are saved.

So use getDefaultSharedPreferences(), do not mess with filenames of shared preferences, read the java doc quoted (applies to the PreferenceActivity more or less) and have a look at :

Community
  • 1
  • 1
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361