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, DhruwatYou 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.
精彩评论