Skip to content Skip to sidebar Skip to footer

Android Custom Sharedpreferences Implementation To Persist To Database?

I am attempting to use a custom implementation of the SharedPreferences interface to persist an application's preferences to a database (instead of the default XML). Why? I'd like

Solution 1:

I've been investigating this question some time ago. For a different reason though.

What I found is that PreferenceActivity is highly coupled with PreferenceManager and it uses PreferenceManager.getDefaultSharedPreferences() deep inside to get SharedPreference instance. And there is no way to either substitute custom PreferenceManager or SharedPreference instance inside PrefernceActivity.

I found that Preference framework is inflexible and it's quite hard to extend or change default behaviors, and sometimes its even impossible. But my guess is that designers of this this framework had "ease of use" as their top priority, not extendability and flexibility. Which is understandable.

Solution 2:

After some research and having that very particular need, there is a "simple" solution.

One will need to override both SharedPreferences and Editor to provides its own implementation, then every Preferences must also be extended to override the various persistXXX() and getPersistedXXX() methods.

For example, the CheckBoxPreference can override persistBoolean and getPersistedBoolean to do the trick.

Sadly it seems impossible to simply extendsthe PreferenceManager class to then only override getPreferenceManager() in each Preference classes.

Solution 3:

I found an easy work-around: just set all preferences to persistent="false" (either via XML or via preference.setPersistent(false) in the code). Then, setup OnPreferenceChangeListener's to get notified when preferences are changed and let the handler store the values in some custom database.

Here is my code:

Preference preference = findPreference(getString(R.string.pref1));
Preference.OnPreferenceChangeListener changeListener = newPreference.OnPreferenceChangeListener() {
        @OverridepublicbooleanonPreferenceChange(Preference preference, Object value) {
            preference.setSummary((String)value + " something");

            // store value in custom databasestore((String)value);

            returntrue;
        }
    };
    preference.setOnPreferenceChangeListener(changeListener);
    // update preference from custom database (once)
    changeListener.onPreferenceChange(preference, getValueFromDatabase());

Solution 4:

I'm working on preferences which are stored on a Bluetooth device. My solution is to override the getSharedPreferences method of my Activity:

@Overridepublic SharedPreferences getSharedPreferences(String name, int mode) {
    returnnewMySharedPreferences(this);
}

MySharedPreference is my implementation of the android.content.SharedPreferences interface. You must also implement it's inner classes. In the MySharedPreference constructer the activity is passed as parameter and stored in a member variable

Post a Comment for "Android Custom Sharedpreferences Implementation To Persist To Database?"