Save a key value pair collection Previous lesson next lesson this lesson teaches you
- Get a handle to a sharedpreferences (Handle)
- Write Sharing preferences
- Read Sharing preferences
You also need to read
- Using the Sharing preferences
If you have a relatively small set of key values that need to be saved, you should use the SharedPreferences
API. An SharedPreferences
object points to a file that contains key-value pairs and provides a simple way to read and write key-value pairs. Each SharedPreferences
file is managed by the framework, and it can be private or shared.
This lesson teaches you how to use SharedPreferences
the API to store and retrieve simple values.
Note: SharedPreferences
APIs are only used to read and write key-value pairs, and you should not confuse them with Preference
APIs, and the Preference API helps you build the user interface for your app's settings (although they use it SharedPreferences
as their implementation to save app settings). For information on using Preference
the API, see the Setup Guide.
Get a handle to a sharedpreferences (Handle)
You can call one of the following two methods to create a new shared preference file or use an existing file:
getSharedPreferences()
-If you need multiple preference files that are differentiated by name, use this method to specify a name with the first parameter. You can call this method arbitrarily in your app Context
.
getPreferences()
-If you only need to use a shared preference file for an activity, you can Activity
call this method from that. Because this method retrieves a default sharing preference file that belongs to that activity, you do not need to provide a name.
For example, the following code is Fragment
executed in one. It accesses a R.string.preference_file_key
shared preference file specified by the resource string, opens and uses the file in private mode, and the file is accessible only to your app.
Context Context = getactivity();sharedpreferences Sharedpref = Context.getsharedpreferences( getString(R.string.Preference_file_key), Context.mode_private);
When naming your shared preferences file, you should use a name that your app can uniquely identify, such as"com.example.myapp.PREFERENCE_FILE_KEY"
Or, if your activity requires only a shared preference file, you can use the getPreferences()
method:
sharedpreferences Sharedpref = getactivity (). getpreferences (Context. mode_private );
Warning: If you use MODE_WORLD_READABLE
or MODE_WORLD_WRITEABLE
create a shared preferences file, any other apps that know about this file identifier can access your data.
Write Sharing preferences
In order to write a shared preference file, SharedPreferences
create one on your raise edit()
SharedPreferences.Editor
.
Pass in the keys and values you want to write with this method, such as putInt()
and putString()
. Then call commit()
save 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 Sharing preferences
To retrieve a value from a shared preference file, call the method, for example, getInt()
or getString()
provide the default value that you want to return when the key and an optional option do 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);
Last lesson Next lesson
Android Training-Start-save data-save key-value pairs collection