Skip to content

Instantly share code, notes, and snippets.

@Bahaaib
Last active June 11, 2017 22:22
Show Gist options
  • Select an option

  • Save Bahaaib/2b9b9e6820da44b820079c94601f35fc to your computer and use it in GitHub Desktop.

Select an option

Save Bahaaib/2b9b9e6820da44b820079c94601f35fc to your computer and use it in GitHub Desktop.
How to store a small amount of data using Shared Preferences
Add the following lines to onCreate method..
SharedPreferences sharedPreferences = this.getSharedPreferences("package name", Context.MODE_PRIVATE); //let the mode private to prevent
//..any other App to access into it
sharedPreferences.edit().putString("variable_name", "variable_value").apply(); //@ putString() it could be any other variable type.
String variable_name = sharedPreferences.getString("variable_name",""); //"" refers to a default value for variable_name in case there wasn't
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment