1

I have an Android application with more source packages. In the base package I have a PreferenceActivity used to configure the preferences of the app. From all the activities within the same package as the PreferenceActivity, I can access these preferences by using PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

However, from another package, activities use another shared preferences file when I call PreferenceManager.getDefaultSharedPreferences(getApplicationContext());. This stops me from accessing the preferences from the PreferenceActivity.

How can I solve this? I want to retrieve the same shared preferences in every package in the application.

Gabriel
  • 2,054
  • 4
  • 26
  • 34

1 Answers1

1

Can't you just use a name for the shared preference and then get the same name for all the applications? Look under shared preferences at: http://developer.android.com/guide/topics/data/data-storage.html#pref

Edit: I think what you look after is here, no reason to take out parts as it gives a pretty good explanation: How do I get the SharedPreferences from a PreferenceActivity in Android?

Community
  • 1
  • 1
Warpzit
  • 27,966
  • 19
  • 103
  • 155
  • Normally I would. But since I'm using a PreferenceActivity, I cannot do this, as it seems that one cannot give a name to the SharedPreferences used by a PrefenceActivity – Gabriel Dec 11 '11 at 19:07
  • Well you could always call a new activity from your shared activity and then make that activity save the content :S but seems like an awfull hack. I can't see why a preferenceactivity should be any different from a regular activity, the real problem might be that you can't use shared preferences in this way.... – Warpzit Dec 12 '11 at 07:46
  • If you can't make this work I suggest using another way for data storage. – Warpzit Dec 12 '11 at 07:47
  • True. Another approach that I thought of would be to simply register a listener for each Preference in the PreferenceActivity. When the preference value is changed, I'd also change that preference into my SharedPreferences. – Gabriel Dec 12 '11 at 18:45
  • Something like this: `checkBoxPreference.setOnPreferenceClickListener(new MyListener());`. In the body of onPreferenceClick() in MyListener, I write something like this: `getSharedPreferences(MY_PREFS_NAME, Preferences.WORLD_WRITABLE).edit().putBoolean(checkBoxPreference.getKey(), checkBoxPreference.isChecked()).commit();` Still, this is an undesirable hack, since I have the same values stored twice, i.e. they get saved in 2 preference files – Gabriel Dec 12 '11 at 18:49
  • Updated answer with link to a perfect answer :) – Warpzit Dec 13 '11 at 10:55