views:

46

answers:

2

Hi, two activities here, A and B. A is main, B is PreferenceActivity with two EditTextPreference.

I want to be able to access the preferences generated by B from activity A, and for that I need of course (I guess) getSharedPreferences on A. But in order to do that I need to supply the preference file for the activity that generated it. This is tedious (long names), erratic at best (depends on the activity that generated it), and finally it's a mess, because it's much better to keep everything under one xml file, with a name that is well known to all my activities. And besides, if I have 100 activities, how would I keep track of preference files anyway...

So the point is: how do I "bind" an EditTextPreference in a way that the changes are made to a given preference file, not the one automatically generated by Android?

+1  A: 

I'm not quite sure why you want to use a specific preferences file. Why not the default one?

SharedPreferences pm = PreferenceManager.getDefaultSharedPreferences(this); gives you the shared preferences that are valid all across your application. What was written in your PreferenceActivity will be in there.

EboMike
Yes, it worked, thank you. I was trying getPreferences and getSharedPreferences and neither worked. The first would give me different file names (depending on the activity I was in) and to use the second I needed to know in advance the full package+class name (which is the file name), and it wouldn't work for different preferences activities, that would generate prefs automatically according to each EditTextPreferences.
David
Glad to help! Could I ask you to accept the answer? That way we both get rep points. Thanks!
EboMike
Isn't the green tick the way to do that? I'm really confused, I'm new here. If you mean vote up, I tried, but I don't have enough rep. Ah, and you are the author of EboBirthday, right? Wow...
David
No, the green tick is fine! (It wasn't accepted yet when I wrote the comment, that's why I asked. Sorry, didn't mean to push you :) it's common for first-time posters to not know about this whole accepting answer business, so I just wanted to point it out.)
EboMike
A: 

Use a static string in one of your classes

Falmarri
I tried that using getSharedPreferences. It would work for me, considering that I used EditTextPreferences from only one PreferenceActivity. That way I could know the package+class name and access statically from all others. However, it's not the most elegant, considering I could add other PreferenceActivity'ies later, and each EditTextPreference change settings to their own activity preference file. It would only increase complexity... I was ending with a bunch of "package_activityname.xml" files that way, and needed to remember each. Anyway, thank you very much for helping me! :-)
David
Wait, do you want preferences for individual activities? It's really confusing what you want exactly.
Falmarri
I have only one PreferenceActivity, and I don't need individual files, even though there is one for each PreferenceActivity. I just wanted to know if there was a way to access ALL preferences from all activities in a centralized way, without needing to know the name of each generated xml file (under /data/data/package/shared_prefs). I didn't know that even existed, so I asked if I could, *at least*, redirect the EditTextPreferences to a previously known xml file.
David