Is there a way to set global preference file for PreferenceManager

27 Views Asked by At

I'm using Android PreferenceFragment for a multi-screen preference solution. I need to have custom preference files because I have per-user settings and need them in different files.

Previously, I had one big xml file for all screens and only one PreferenceFragment. That is, however, not supported anymore, which is a real drag, because now I have to have a PreferenceFragment for each screen and juggle between them.

(read about them removing support for a single xml file here: https://developer.android.com/develop/ui/views/components/settings/organize-your-settings )

It also seems I have to remember to call the getPreferenceManager().setSharedPreferencesName(PREFS_FILE_NAME); for every screen that wants to write to the preference file.

Is there not any "global" setting for this? I would like to set it once and then forget it.

Sometimes stuff feels so needlessly complicated on Android.

My code to change the config file for the manager:

protected void setPrefsFile() {
    //the policy business is so that strictmode doesn't complain about something we have to do here, i.e. load from disk on main thread.
    StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
    try {
        getPreferenceManager().setSharedPreferencesName(PREFS_FILE_NAME);
    } catch (Exception e) {
        Log.e(TAG, "could not set prefs name: " + e.toString());
    }finally{
        StrictMode.setThreadPolicy(oldPolicy);
    }
}
0

There are 0 best solutions below