Skip to content

Instantly share code, notes, and snippets.

@fethica
Created October 15, 2018 14:21
Show Gist options
  • Save fethica/1dc8feefc9de5e36fe91365cb8988789 to your computer and use it in GitHub Desktop.
Save fethica/1dc8feefc9de5e36fe91365cb8988789 to your computer and use it in GitHub Desktop.
// Source: https://stackoverflow.com/questions/17753800/android-default-values-for-shared-preferences
public class Preferences {
public static final String MY_PREF = "MyPreferences";
private SharedPreferences sharedPreferences;
private Editor editor;
public Preferences(Context context) {
this.sharedPreferences = context.getSharedPreferences(MY_PREF, 0);
this.editor = this.sharedPreferences.edit();
}
public void set(String key, String value) {
this.editor.putString(key, value);
this.editor.commit();
}
public String get(String key) {
return this.sharedPreferences.getString(key, null);
}
public void clear(String key) {
this.editor.remove(key);
this.editor.commit();
}
public void clear() {
this.editor.clear();
this.editor.commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment