1page.title=Saving Key-Value Sets 2 3trainingnavtop=true 4 5@jd:body 6 7 8<div id="tb-wrapper"> 9<div id="tb"> 10 11<h2>This lesson teaches you to</h2> 12<ol> 13 <li><a href="#GetSharedPreferences">Get a Handle to a SharedPreferences</a></li> 14 <li><a href="#WriteSharedPreference">Write to Shared Preferences</a></li> 15 <li><a href="#ReadSharedPreference">Read from Shared Preferences</a></li> 16</ol> 17 18<h2>You should also read</h2> 19<ul> 20 <li><a href="{@docRoot}guide/topics/data/data-storage.html#pref">Using Shared Preferences</a></li> 21</ul> 22 23</div> 24</div> 25 26 27<p>If you have a relatively small collection of key-values that you'd like to save, 28you should use the {@link android.content.SharedPreferences} APIs. 29A {@link android.content.SharedPreferences} object points to a file containing 30key-value pairs and provides simple methods to read and write them. Each 31{@link android.content.SharedPreferences} file is 32managed by the framework and can be private or shared.</p> 33 34<p>This class shows you how to use the {@link android.content.SharedPreferences} APIs to store and 35retrieve simple values.</p> 36 37<p class="note"><strong>Note:</strong> The {@link android.content.SharedPreferences} APIs are 38only for reading and writing key-value pairs and you should not confuse them with the 39{@link android.preference.Preference} APIs, which help you build a user interface 40for your app settings (although they use {@link android.content.SharedPreferences} as their 41implementation to save the app settings). For information about using the {@link 42android.preference.Preference} APIs, see the <a href="{@docRoot}guide/topics/ui/settings.html" 43>Settings</a> guide.</p> 44 45<h2 id="GetSharedPreferences">Get a Handle to a SharedPreferences</h2> 46 47<p>You can create a new shared preference file or access an existing 48one by calling one of two methods:</p> 49<ul> 50 <li>{@link android.content.Context#getSharedPreferences(String,int) 51getSharedPreferences()} — Use this if you need multiple shared preference files identified 52by name, which you specify with the first parameter. You can call this from any 53{@link android.content.Context} in your app.</li> 54 <li>{@link android.app.Activity#getPreferences(int) getPreferences()} — Use this from an 55{@link android.app.Activity} if you need 56to use only one shared preference file for the activity. Because this retrieves a default shared 57preference file that belongs to the activity, you don't need to supply a name.</li> 58</ul> 59 60<p>For example, the following code is executed inside a {@link android.app.Fragment}. 61It accesses the shared preferences file that's 62identified by the resource string {@code R.string.preference_file_key} and opens it using 63the private mode so the file is accessible by only your app.</p> 64 65<pre> 66Context context = getActivity(); 67SharedPreferences sharedPref = context.getSharedPreferences( 68 getString(R.string.preference_file_key), Context.MODE_PRIVATE); 69</pre> 70 71<p>When naming your shared preference files, you should use a name that's uniquely identifiable 72to your app, such as {@code "com.example.myapp.PREFERENCE_FILE_KEY"}</p> 73 74<p>Alternatively, if you need just one shared preference file for your activity, you can use the 75{@link android.app.Activity#getPreferences(int) getPreferences()} method:</p> 76 77<pre> 78SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 79</pre> 80 81<p class="caution"><strong>Caution:</strong> If you create a shared preferences file 82with {@link android.content.Context#MODE_WORLD_READABLE} or {@link 83android.content.Context#MODE_WORLD_WRITEABLE}, then any other apps that know the file identifier 84can access your data.</p> 85 86 87<h2 id="WriteSharedPreference">Write to Shared Preferences</h2> 88 89<p>To write to a shared preferences file, create a {@link 90android.content.SharedPreferences.Editor} by calling {@link 91android.content.SharedPreferences#edit} on your {@link android.content.SharedPreferences}.</p> 92 93<p>Pass the keys and values you want to write with methods such as {@link 94android.content.SharedPreferences.Editor#putInt putInt()} and {@link 95android.content.SharedPreferences.Editor#putString putString()}. Then call {@link 96android.content.SharedPreferences.Editor#commit} to save the changes. For example:</p> 97 98<pre> 99SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 100SharedPreferences.Editor editor = sharedPref.edit(); 101editor.putInt(getString(R.string.saved_high_score), newHighScore); 102editor.commit(); 103</pre> 104 105 106<h2 id="ReadSharedPreference">Read from Shared Preferences</h2> 107 108<p>To retrieve values from a shared preferences file, call methods such as {@link 109android.content.SharedPreferences#getInt getInt()} and {@link 110android.content.SharedPreferences#getString getString()}, providing the key for the value 111you want, and optionally a default value to return if the key isn't 112present. For example:</p> 113 114<pre> 115SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 116int defaultValue = getResources().getInteger(R.string.saved_high_score_default); 117long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue); 118</pre> 119 120