page.title=Saving Key-Value Sets page.tags=data storage helpoutsWidget=true trainingnavtop=true @jd:body
If you have a relatively small collection of key-values that you'd like to save, you should use the {@link android.content.SharedPreferences} APIs. A {@link android.content.SharedPreferences} object points to a file containing key-value pairs and provides simple methods to read and write them. Each {@link android.content.SharedPreferences} file is managed by the framework and can be private or shared.
This class shows you how to use the {@link android.content.SharedPreferences} APIs to store and retrieve simple values.
Note: The {@link android.content.SharedPreferences} APIs are only for reading and writing key-value pairs and you should not confuse them with the {@link android.preference.Preference} APIs, which help you build a user interface for your app settings (although they use {@link android.content.SharedPreferences} as their implementation to save the app settings). For information about using the {@link android.preference.Preference} APIs, see the Settings guide.
You can create a new shared preference file or access an existing one by calling one of two methods:
For example, the following code is executed inside a {@link android.app.Fragment}. It accesses the shared preferences file that's identified by the resource string {@code R.string.preference_file_key} and 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 should use a name that's uniquely identifiable to your app, such as {@code "com.example.myapp.PREFERENCE_FILE_KEY"}
Alternatively, if you need just one shared preference file for your activity, you can use the {@link android.app.Activity#getPreferences(int) getPreferences()} method:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
Caution: If you create a shared preferences file with {@link android.content.Context#MODE_WORLD_READABLE} or {@link android.content.Context#MODE_WORLD_WRITEABLE}, then any other apps that know the file identifier can access your data.
To write to a shared preferences file, create a {@link android.content.SharedPreferences.Editor} by calling {@link android.content.SharedPreferences#edit} on your {@link android.content.SharedPreferences}.
Pass the keys and values you want to write with methods such as {@link android.content.SharedPreferences.Editor#putInt putInt()} and {@link android.content.SharedPreferences.Editor#putString putString()}. Then call {@link android.content.SharedPreferences.Editor#commit} 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();
To retrieve values from a shared preferences file, call methods such as {@link android.content.SharedPreferences#getInt getInt()} and {@link android.content.SharedPreferences#getString getString()}, 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);