Android SharedPreferences: How to define keys at runtime? - android

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());

Related

AndroidX multiple instances of Preferences

How can I keep multiple sets of Preferences using the AndroidX Perference library?
For example, the user can choose different profiles which have different values over the same set of settings:
Global preferences:
Selected profile: foo
Other settings: etc
Profile 1:
Name: foo
Setting: Bar
Profile 2:
Name: bar
Setting: Hello
etc
I see two options here:
Use Context.getSharedPreferences(String name, int mode) with different names for different profiles. PreferenceManager has a setDefaultValues method that also takes a name in that case. The downside of this approach is that a settings screen will still use the default shared prefs, and not the ones for whatever profile is selected.
Make your own shared prefs wrapper that extends PreferenceDataStore and implements all the methods. Then call PreferenceManager.setPreferenceDataStore whenever the profile is changed. A settings screen will show the correct values with this approach. Internally, your wrapper can either use a database to store the values, or use the default shared prefs, appending a different prefix for different profiles.
I haven't tried either but I believe it could work.

Set Android preference file that my Preference objects save to?

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..

How to attach a Preference to a particular SharedPreferences?

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

Load preferences according to application mode

I would like to load XML preferences according to application mode, like DEBUG, TEST or PRODUCION where different values are entered for the same keys.
Additionally the preferences default values must be set on start up.
This will allow easy testing in different circumstances.
Which is the best way to accomplish this.
PS: I don't want the user to see or have the option to change this settings.
You can add PreferenceCategory with say testCategory key and place all test related preferences tehere.
And if current mode is PRODUCTION, then just remove this testCategory from preferences in onCreate() function:
if(isProduction())
{
Preference testCategory = findPreference("testCategory");
getPreferenceScreen().removePreference(testCategory);
}

Dynamic preferences for a variable number of profiles in an Android app

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.

Categories

Resources