RichardNewton
RichardNewton

Reputation: 886

Saving and restoring sets of preferences

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?

Upvotes: 0

Views: 276

Answers (1)

Barmaley
Barmaley

Reputation: 16363

IMHO basically there are three ways, how to achieve your goals:

  • Create set of SharedPreferences - separate for each profile, like:
    settingsCommon=context.getSharedPreferences("MyCommon", MODE_PRIVATE);
    settingsLowBattery=context.getSharedPreferences("MyLowBattery", MODE_PRIVATE);
  • Add to each preference key smth like 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);
}
  • Extend SharedPreferences class and implement your own, including creating/parsing of any kind XML

Upvotes: 1

Related Questions