In my application I am trying to add support for different profiles. Each of these profiles can have their own independent Preferences, configured from a deep tree of custom and stock preference objects.
I thought that I could add this easily by using different Preference Files per profile, as in getSharedPreferences("george", MODE_PRIVATE); would store them for profile "george".
The issue I am having is the Preference objects seem to be storing their values in the default Preference file. Is there any way from my Activity or Fragment to change which Preference file is used by the Preferences on my Preference Screen? I know this doesn't exist, but I'm looking for something like .setPreferenceFile(String filename) for a Preference or PreferenceGroup.
Any help or suggestions to minimize the work to support independent profile preferences will be appreciated..
Related
The Android documentation describes how to create a "preferences" UI using either a PreferenceActivity or a PreferenceFragment. The preferences themselves are defined in a XML file (e.g. preferences.xml) which contain, among other things, the "key" (android:key) to use to store each preference in the app's SharedPreferences.
This is good for apps which have a single set of preferences. However let's assume that there is an app that can have multiple "items" (for example: multiple account in a video chat app), and needs to show a preferences screen for each account.
How can this be done? Is it possible to specify the keys at runtime (so that e.g a different prefix can be used for each account)? Is there a better way to approach this?
Yes, its possible!
you can assign keys on Runtime like, but as you said in comments that you need to change the Keys of the pre-defined Preferences in Xml rather than creating new Preferences explicitly, here's a method -
Preference pref = findPreference("my_pref");
String myPrefix = "prefix_";
pref.setKey(myPrefix + pref.getKey());
I need to create an activity that must be a lot like a settings activity, with a list of clickable items.
What I need is a set of view like CheckBoxPreference, EditTextPreference and DialogPreference in order to let the user set some values.
Anyway I don't want to save the values inside SharedPreference. I need to save them inside another object.
Which is the best way to do this?
If you do not want to save the value in `SharedPreference.
Alternative options.
1 Sqlite
2 File in internal or external memory.
I recommend to use SharedPreferences
The easiest way would be to use a settings activity, let it save to shared preferences, populate your object from shared preferences, then clear the shared preferences.
I can read and write shared preferences and have verified that the resulting XML file looks correct. But is it possible to build a hierarchy of preferences instead of just a flat list?
I'm accessing the preferences directly from my code, so the solution must not assume a preferences screen is present in the app. I have found the PreferenceGroup, PreferenceCategory, and PreferenceManager classes, but they seem to assume a preferences screen is present.
But is it possible to build a hierarchy of preferences instead of just a flat list?
No, sorry. From the rest of your question, it's unclear why you are using SharedPreferences in the first place, instead of another persistent data model (database, XML file, JSON file, etc.)
I am working on implementing the preferences for our application. I know how to display preferences UI and how to read/write values using SharedPreferences. In our app, I need to handle two sets of preferences and I would like to ask about this issue, one comment in the Android documents in particular.
The documentation for Preference.getSharedPreferences() has the following comment under the Return values section:
Returns The SharedPreferences where this Preference reads its value(s), or null if it isn't attached to a Preference hierarchy.
I would like to ask how it is possible to attach a SharedPreferences to a particular Preference, be it EditTextPreference or others. In other words, how does the persistence code in a Preference know that it should store the user input in one particular SharedPreferences object and not the other?
To explain my question further with an example, suppose I have the following:
SharedPreferences prefs1 = getSharedPreferences(file1, mode);
SharedPreferences prefs2 = getSharedPreferences(file2, mode);
My question is what API I should use so that prefs1 is used by the Preference objects' persistence code and not prefs2.
The target is Nexus One, running 2.3.4.
Maybe the answer is obvious but I could not find it after reading the documentation and searching the web. Thank you in advance for your help.
In other words, how does the persistence code in a Preference know that it should store the user input in one particular SharedPreferences object and not the other?
Preference uses PreferenceManager's getSharedPreferences(), which eventually routes to getDefaultSharedPreferences().
You are welcome to create your own Preference subclasses that change this behavior, but since the preference screen system may not be designed to handle multiple SharedPreference objects, there's a chance that your preference changes might not get persisted.
IOW, I encourage you to reconsider:
In our app, I need to handle two sets of preferences
I am looking for a way to create dynamic preferences where I don't need to hard code the preference key and I could have a variable number of preferences.
Basically, my application will let the user create a multiple number of profiles and each of these profiles will save custom values for a fixed number of preferences.
So this way, the user does not have to change the preferences every time he wants this app to run differently, he can just switch the profile.
One way I think will work is by subclassing all the standard Preference classes and calling their setKey method with my custom preference key, containing the profile name, but this is ugly.
So is there a cleaner and more standards compliant way to do this?
Regards,
Dhruwat
You can save different preferences in a different file for each user using the getSharedPreferences method:
getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.
That way, you can do something like this:
SharedPreferences settings = getSharedPreferences("prefs_user_"+user_id, 0);
// from now on you can use use the normal way to set or get the preferences
I'm assuming you are using an id in order to identify them users.