Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? I.e. 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

like image 632
barry Avatar asked Nov 06 '11 12:11

barry


1 Answers

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()!

like image 137
stephane Avatar answered Oct 22 '22 22:10

stephane