Skip to content

Instantly share code, notes, and snippets.

@bashizip
Created February 6, 2017 14:01
Show Gist options
  • Save bashizip/6f8e1260b4b7cff2cd0cb1200e483d64 to your computer and use it in GitHub Desktop.
Save bashizip/6f8e1260b4b7cff2cd0cb1200e483d64 to your computer and use it in GitHub Desktop.
Simple class for easily Using Preferences in Android
import android.content.Context;
import android.content.ContextWrapper;
import android.content.SharedPreferences;
import android.support.compat.BuildConfig;
/**
*
* Created by bashizip@gmail.com
*/
public class BashPreferencesManager extends ContextWrapper {
public static final String PREF_NAME = BuildConfig.APPLICATION_ID;
private static BashPreferencesManager instance;
private SharedPreferences pm;
private SharedPreferences.Editor editor;
private BashPreferencesManager(Context base) {
super(base);
pm = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
}
public static BashPreferencesManager getInstace(Context ctx) {
if (instance == null) {
instance = new BashPreferencesManager(ctx);
}
return instance;
}
private SharedPreferences.Editor getEditor() {
if (editor == null) {
editor = pm.edit();
}
return editor;
}
public BashPreferencesManager put(String key, String value) {
getEditor().putString(key, value);
return this;
}
public void commit() {
getEditor().apply();
}
public String getValue(String key) {
return pm.getString(key, null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment