Are there any nice ways to save and then restore the values for a group of preferences (i.e. stored using SharedPreferences) in Android?
For my game app, I have about 10 preferences concerning how the game should be rendered. What I’d like to do is add various profiles that can be selected, where each profile would set the values of the 10 preferences. For example, I might have a “low battery usage” profile that would set all the rendering preferences to values that use the least amount of battery or a “high detail” profile to set all the rendering preferences to their highest settings.
I will also have several preference that are not set by these profiles. For example, I will have a preference that stores the name of the current active profile.
What options do I have about how I save/restore profiles? How would changing profiles be implemented?
IMHO basically there are three ways, how to achieve your goals:
SharedPreferences– separate for each profile, like:settingsCommon=context.getSharedPreferences("MyCommon", MODE_PRIVATE); settingsLowBattery=context.getSharedPreferences("MyLowBattery", MODE_PRIVATE);suffix which will point to profile:
int getIntPreference(SharedPreferences settings, String profile, String key, int defValue) { String fullKey=key+"."+profile; return settings.getInt(fullkey, defValue); }SharedPreferencesclass andimplement your own, including
creating/parsing of any kind XML