Created
February 6, 2017 14:01
-
-
Save bashizip/6f8e1260b4b7cff2cd0cb1200e483d64 to your computer and use it in GitHub Desktop.
Simple class for easily Using Preferences in Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.ContextWrapper; | |
import android.content.SharedPreferences; | |
import android.support.compat.BuildConfig; | |
/** | |
* | |
* Created by [email protected] | |
*/ | |
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