Question

I just implemented a preferences screen for one of the sample apps that I am creating. I followed the guide given on google site. All is fine and I loaded up a ListPreference and I am able to store it and persist it also. There is also one minor problem here. I have already defined a custom preference file for the app but this automatic handling of the preference screeen seems to be creating a preference file on its own. For now I was trying to get this code to work but it is not getting the custom preference file.

        final Preference customPrefs = getPreferenceScreen().findPreference(Utils.PREFS_NAME);

    customPrefs.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {
            customPrefs.getEditor().commit();
            return true;
        }
    });

Is there a way to bind the custom preference file with this auto preference class in anyway ?

Was it helpful?

Solution

You can set a custom name for the file used to store the preferences, used in your PreferenceActivity, by calling its getPreferenceManager().setSharedPreferencesName("file_name") method. Just remember that you need to set that before calling addPreferencesFromResource, otherwise your UI will still change stuff back to the wrong file (the default one).

As I said, you don't need to do that, since you can use the default file provided by the system. If/when you need to read the preferences elsewhere, you can then just call PreferenceManager.getDefaultSharedPreferences(Context context).

I already addressed those remarks in another question.

Also remember that some of the methods are deprecated. If you're writing new code, try to avoid them and conform to the new "Fragment Way" of doing things.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top