Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save MohammadSamandari/7380f605126ece0d7c8a37f78abec41e to your computer and use it in GitHub Desktop.

Select an option

Save MohammadSamandari/7380f605126ece0d7c8a37f78abec41e to your computer and use it in GitHub Desktop.
Permanent Data Storage - Shared Preferences
/*
Permanent Data Storage
There are different ways of storing data on the app, the most simple one iscalled
Shared Preferences.
this is a way of storing just very simple short items of data like username or ideally
a preference that user has choosen for the app.
it is great for storing short information.
*/
SharedPreferences sharedPreferences = this.getSharedPreferences("com.mohammadsamandari.sharedpreferences", Context.MODE_PRIVATE);
// The first bit of information is the apps name
// The second part defines that only this app is going to access this shared preferences.
// To save a item into shared preferences:
sharedPreferences.edit().putString("username", "lord").apply();
// to get back information out of shared preferences.
String username = sharedPreferences.getString("username", "DEFAULT_VALUE_IN_CASE_NOT_AVAILABLE");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment