This article translated from: http://developer.android.com/training/basics/data-storage/shared-preferences.html
If you have a relatively small set of key-value pairs to be saved, you should use sharepreferences
APIS.
The sharepreferences object points to a file containing key-value pairs and provides simple read/write methods. Each sharepreferences file is managed by the framework and can be private or shared.
This section describes how to use sharepreferences APIs to save and obtain simple values.
Note: sharedpreferences APIS is only used to read and write key-value pairs. You cannot confuse them with preference APIs, preference
APIS is used to help you build the user interface for application settings (although they use sharepreference as the implementation for saving application settings ). Preference
For more information about APIs, see the setting guide.
Get the sharedpreferences Processing
You can create a new shared preference file or call one of the following two methods to access a existing preference file:
Getsharedpreferences () --- if you need multiple shared preferences identified by names, the file name is specified using the first parameter of this method. You can call this method from the context object in your application.
Getpreferences () --- if you only use a shared preference file for a specific activity, you can use this method from the activity.
For example, the following code must be executed within a fragment. It will access the shared preference file identified by the resource string R. String. preference_file_key and open the file in private mode. Therefore, this file can only be accessed by your application.
Context context = getactivity ();
Sharedpreferences sharedpref = context. getsharedpreferences (
Getstring (R. String. preference_file_key), context. mode_private );
When naming your shared preference file, you should use the name that is uniquely identified for your application, such as "com. example. MyApp. preference_file_key"
In addition, if you only need to share preference files for a specific activity, you can use the getpreferences () method:
Sharedpreferences sharedpref = getactivity (). getpreferences (context. mode_private );
Warning if you use mode_word_readable or mode_world_writeable to create a shared preference file, other applications that know the file ID can also access your data.
Write shared preference files
To write data to a shared preferences file, you must call the Edit () method on sharedpreferences to create a sharedpreferences. Editor object.
Use methods such as putint () and putstring () to pass the keys and values you want to write to the sharedpreferences object. Call the Commit () method to save the changes. For example:
Sharedpreferences sharedpref
= Getactivity (). getpreferences (context. mode_private );
Sharedpreferences. Editor editor = sharedpref. Edit ();
Editor. putint (getstring (R. String. saved_high_score), newhighscore );
Editor. Commit ();
Read from the shared preference file
To obtain a value from a shared preference file, call methods such as getint () and getstring (), and provide the key names of the values you want to obtain for these methods, and the default value to be returned if the key name does not exist. For example:
Sharedpreferences sharedpref
= Getactivity (). getpreferences (context. mode_private );
Int defaultvalue = getresources (). getinteger (R. String. saved_high_score_default );
Long highscore = sharedpref. getint (getstring (R. String. saved_high_score), defaultvalue );