user5182794
user5182794

Reputation:

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:

Profile 1:

Profile 2:

etc

Upvotes: 0

Views: 69

Answers (1)

Nicolas
Nicolas

Reputation: 7121

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.

Upvotes: 0

Related Questions