-3

I have a set of preference fragments (ringtone, threshold,...) for each entity. So I would rather load the relevant contents from the DB (ContentProvider) than from SharedPrefences. What is the best way to do this? Is there a way to intercept the loading/saving process from/to SharedPreferences?

If possible I would like to avoid a custom layout, because built in preference features are much faster and easier to implement.

Matthias
  • 263
  • 4
  • 11
  • 1
    I don't see why you want to use a database instead of SharedPreferences, can you clarify that a bit? – 1615903 Nov 26 '15 at 10:25
  • Because I have a list of entities that are loaded from the DB (and synced over the net). And the prefs need to be set for each entity individually. – Matthias Nov 26 '15 at 10:28
  • If the prefs need to be set for each entity individually and sent over the internet then you need to make provisions for these in your database schema. – Narayan Acharya Nov 26 '15 at 10:36
  • what exactly do you want to store in DB? the selected values by the user or the possible options the user can pick among? – Yazan Nov 26 '15 at 10:37
  • My users can load a list of places (from the DB) and set e.g. an alarm ringtone for each place. (This is a customer's requirements.) – Matthias Nov 26 '15 at 10:44
  • You can also shoot mosquitoes with a cannon, if you like. – Phantômaxx Nov 26 '15 at 10:51
  • 1
    override `SharedPreferences getSharedPreferences()` in your custom `Preference` so that it returns your own implementation of `SharedPreferences` interface – pskink Nov 26 '15 at 11:14

2 Answers2

1

From a Mobile UX (User experience) perspective this is a really bad idea. Since a DB access yields a bigger UI latency in comparison to the simple key-value-store-accesses the user experience will suffer a great deal from this. Go for the standard approach, and write a convenience wrapper class instead where you can pass in your objects that can contain a whole preference.

0

The purpose of the question was to imitate the look and feel from the built in preferences framework the best possible way.

Thanks @pskink for pointing out how this can be done by implementing your own SharedPreferences!

I finally took this proposal as a starting point for a custom layout because I needed some controls that are not provided out of the box.

Community
  • 1
  • 1
Matthias
  • 263
  • 4
  • 11