Saving information using Shared Preference in Android?

deepak - Sun Feb 19 2012 20:50:43 GMT-0500 (EST)

How to Read the Preference?


1) Get the sharedpreference for the preference name
ex) SharedPreferences prefs = ctx.getSharedPreferences(prefName,0);
2) Get all the key value pairs from the preference
ex) Map data = prefs.getAll();
3) Iterate through the map and you can retrive the data for your key.

deepak - Sun Feb 19 2012 20:53:58 GMT-0500 (EST)

How to Save the Preference?


1) Get the sharedpreference editor for the preference
SharedPreferences.Editor prefs =
context.getSharedPreferences(getPrefname(), 0).edit();

2) add the key and value to the preference
prefs.putString(newkey, value);

3) Commit your changes
prefs.commit();

deepak - Sun Feb 19 2012 20:54:41 GMT-0500 (EST)

How to remove preference?


1)Do all the above steps to get the editor and 
then use the remove method 
to remove the preference.

prefs.remove(newkey);