Return to Snippet

Revision: 42145
at February 28, 2011 08:52 by BenClayton


Initial Code
SharedPreferences prefs = getApplicationContext().getSharedPreferences("YourAppName", 0);
// YourAppName is just a string to identify the prefs. 0 means other apps can't see your prefs.
prefs.getString("lastUrl", ""); // get out a string called 'lastUrl'. "" is the return value if the preference doesn't exist

// To change values you need an 'Editor'
Editor edit = prefs.edit();
edit.putString("lastUrl", "http://www.example.com" ); // save the value..

// could make multiple changes here.
if (!edit.commit()) {
   Log.e("FOO", "Failed to save prefs");
}

Initial URL


Initial Description


Initial Title
Android: Save and restore 'user preference' values using SharedPreferences

Initial Tags
android

Initial Language
Java