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 ). For more information about using Preference APIs, see the Setting guide. For SharedPreferences processing, you can create a new shared preferences file or call one of the following two methods to access an existing preferences file: getSharedPreferences () --- if you need multiple shared preference files 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 a 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 MO DE_WORLD_WRITEABLE to create a shared preference file. Other applications that know the file identity can also access your data. To write a shared preferences file 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. Example: SharedPreferences sharedPref = getActivity (). getPreferences (Context. MODE_PRIVATE); SharedPreferences. editor editor = sharedPref. edit (); editor. putInt (getString (R. string. saved_high_score), newHighScore); editor. commit (); to read the value from the shared preference file, you must call methods such as getInt () and getString () to obtain the value from the shared preference file, provide these methods with the key name you want to obtain the value, and the default value to be returned if the key name does not exist. 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 );