简体   繁体   中英

Using non-default preferences in PreferenceActivity

Using PreferenceActivity, Android automatically saves simple preferences such as checkboxes being checked etc. I have a couple of questions:

1 - where do these preferences get saved? Is it the same preferences file that PreferenceManager.getDefaultSharedPreferences(Context) returns?

2 - is there a way to use a difference set of preferences? Ie with context.getSharedPreferences (String name, int mode) you supply a string to identify a particular set of preferences. Is it possible to save preferences from a PreferenceActivity in a set of preferences returned like this?

Thanks in advance, Barry

Yes it's possible.

Have a look at that : https://idlesun.blogspot.com/2012/12/how-to-make-preferenceactivity-use-non.html

public class MyPreferencesActivity extends PreferenceActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        PreferenceManager prefMgr = getPreferenceManager();
        prefMgr.setSharedPreferencesName("my_preferences");
        prefMgr.setSharedPreferencesMode(MODE_WORLD_READABLE);

        addPreferencesFromResource(R.xml.preferences);
    }
}

addPreferencesFromResource() have to be called after setSharedPreferencesName() !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM