Skip to content

Instantly share code, notes, and snippets.

@captswag
Last active August 29, 2015 14:20
Show Gist options
  • Save captswag/90a804a4cb41cadc51f1 to your computer and use it in GitHub Desktop.
Save captswag/90a804a4cb41cadc51f1 to your computer and use it in GitHub Desktop.
Simplest example of using SharedPreferences in Android to store key-pair values.
String message = "Hi";
static String fileName = "shared_string";
SharedPreferences sharedPreferences;
sharedPreferences = getSharedPreferences(fileName, MODE_PRIVATE);
//Inserting
SharedPreferences.Editor = sharedPreferences.edit();
editor.putString("message", message);
editor.commit();
//Retrieving
String defaultValue = "Nothing found";
String message = sharedPreferences.getString("message", defaultValue);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment