I created SharedPreference obj using following:
Note : creating number of preferences under the same pref file name
public static Sting name = "abx"; //pref file name
spo = context.getsharedpreference(name , mode_private);
editor = spo.edit(); //get the editor associated with pref obj
Map<String, String> test= new HashMap<String, String>();//create a map
test.put("1", "abc");//fill the values
test.put("2", "efg");//used map because values are stored in key -value pairs
test.put("3", "ghj");
for (String s : test.keySet()) {
// use the name as the key, and the icon as the value
editor.putString(s, nameIcons.get(s));//use the key as preference name
}
editor.commit();//commit the preferences
I have a doubt , please correct me if i am wrong , SharedPreference file with name “abz” is created and in this file preference values are stored as key – value pairs or if editor obj creates new preference file for each key , whats the use of supplying pref file name at the time of sharedprefences object creation?
Essentially, you can create multiple preference files for a given application. There are a few reasons one could imagine doing this, the best being if you had multiple accounts. You could label one set of preferences “account1” and another “account2”. Just by keeping track of which account you are in, you can share code without a huge amount of complexity.
Unless you are specifically using this functionality, I suggest you use
context.getApplicationContext().getPackageName()for the name, as it should be available almost everywhere.