If you have a relatively small collection of key-values that you 'd like to save, you should useSharedPreferencesAPIS.SharedPreferencesObject points to a file containing key-value pairs and provides simple methods to read and write them. EachSharedPreferencesFile is managed by the framework and can be private or shared.
This class shows you how to useSharedPreferencesAPIS to store and retrieve simple values.
Note:TheSharedPreferencesAPIs are only for reading and writing key-value pairs and you shoshould not confuse them withPreferenceAPIS, which help you build a user interface for your app settings (although they useSharedPreferencesAs their implementation to save the app settings). For information about usingPreferenceAPIS, see the settings guide.
Get a handle to a sharedpreferences
You can create a new shared preference file or access an existing one by calling one of two methods:
getSharedPreferences()-Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from anyContextIn your app.
getPreferences()-Use this fromActivityIf you need to use only one shared preference file for the activity. Because this retrieves a default shared preference file that belongs to the activity, you don't need to supply a name.
For example, the following code is executed insideFragment. It accesses the shared preferences file that's identified by the resource stringR.string.preference_file_keyAnd opens it using the private mode so the file is accessible by only your app.
Context context = getActivity();SharedPreferences sharedPref = context.getSharedPreferences( getString(R.string.preference_file_key), Context.MODE_PRIVATE);
When naming your shared preference files, you shocould use a name that's uniquely identifiable to your app, such"com.example.myapp.PREFERENCE_FILE_KEY"
Alternatively, if you need just one shared preference file for your activity, you can usegetPreferences()Method:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
Caution:If you create a shared preferences fileMODE_WORLD_READABLEOrMODE_WORLD_WRITEABLE, Then any other apps that know the file identifier can access your data.
Write to shared preferences
To write to a shared preferences file, createSharedPreferences.EditorBy callingedit()On yourSharedPreferences.
Pass the keys and values you want to write with methods suchputInt()AndputString(). Then callcommit()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 shared preferences
To retrieve values from a shared preferences file, call methods suchgetInt()AndgetString(), Providing the key for the value you want, and Optionally a default value to return if the key isn't present. 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);
Saving key-Value Sets