1 page.title=Saving Key-Value Sets 2 3 trainingnavtop=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, 28 you should use the {@link android.content.SharedPreferences} APIs. 29 A {@link android.content.SharedPreferences} object points to a file containing 30 key-value pairs and provides simple methods to read and write them. Each 31 {@link android.content.SharedPreferences} file is 32 managed 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 35 retrieve simple values.</p> 36 37 <p class="note"><strong>Note:</strong> The {@link android.content.SharedPreferences} APIs are 38 only 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 40 for your app settings (although they use {@link android.content.SharedPreferences} as their 41 implementation to save the app settings). For information about using the {@link 42 android.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 48 one by calling one of two methods:</p> 49 <ul> 50 <li>{@link android.content.Context#getSharedPreferences(String,int) 51 getSharedPreferences()} — Use this if you need multiple shared preference files identified 52 by 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 56 to use only one shared preference file for the activity. Because this retrieves a default shared 57 preference 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}. 61 It accesses the shared preferences file that's 62 identified by the resource string {@code R.string.preference_file_key} and opens it using 63 the private mode so the file is accessible by only your app.</p> 64 65 <pre> 66 Context context = getActivity(); 67 SharedPreferences 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 72 to 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> 78 SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 79 </pre> 80 81 <p class="caution"><strong>Caution:</strong> If you create a shared preferences file 82 with {@link android.content.Context#MODE_WORLD_READABLE} or {@link 83 android.content.Context#MODE_WORLD_WRITEABLE}, then any other apps that know the file identifier 84 can 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 90 android.content.SharedPreferences.Editor} by calling {@link 91 android.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 94 android.content.SharedPreferences.Editor#putInt putInt()} and {@link 95 android.content.SharedPreferences.Editor#putString putString()}. Then call {@link 96 android.content.SharedPreferences.Editor#commit} to save the changes. For example:</p> 97 98 <pre> 99 SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 100 SharedPreferences.Editor editor = sharedPref.edit(); 101 editor.putInt(getString(R.string.saved_high_score), newHighScore); 102 editor.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 109 android.content.SharedPreferences#getInt getInt()} and {@link 110 android.content.SharedPreferences#getString getString()}, providing the key for the value 111 you want, and optionally a default value to return if the key isn't 112 present. For example:</p> 113 114 <pre> 115 SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 116 int defaultValue = getResources().getInteger(R.string.saved_high_score_default); 117 long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue); 118 </pre> 119 120