Writing Singleton Class To Manage Android SharedPreferences
Kotlin solution:
object PrefsHelper {
private lateinit var prefs: SharedPreferences
private const val PREFS_NAME = "params"
const val ID_USER = "id_user"
const val TOKEN = "token"
fun init(context: Context) {
prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
}
fun read(key: String, value: String): String? {
return prefs.getString(key, value)
}
fun read(key: String, value: Long): Long? {
return prefs.getLong(key, value)
}
fun write(key: String, value: String) {
val prefsEditor: SharedPreferences.Editor = prefs.edit()
with(prefsEditor) {
putString(key, value)
commit()
}
}
fun write(key: String, value: Long) {
val prefsEditor: SharedPreferences.Editor = prefs.edit()
with(prefsEditor) {
putLong(key, value)
commit()
}
}
}
Call init()
function on first app’s run.
Proper Singleton Shared Preferences Class. it may help others in the future.
public class SharedPref
{
private static SharedPreferences mSharedPref;
public static final String NAME = "NAME";
public static final String AGE = "AGE";
public static final String IS_SELECT = "IS_SELECT";
private SharedPref()
{
}
public static void init(Context context)
{
if(mSharedPref == null)
mSharedPref = context.getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
}
public static String read(String key, String defValue) {
return mSharedPref.getString(key, defValue);
}
public static void write(String key, String value) {
SharedPreferences.Editor prefsEditor = mSharedPref.edit();
prefsEditor.putString(key, value);
prefsEditor.commit();
}
public static boolean read(String key, boolean defValue) {
return mSharedPref.getBoolean(key, defValue);
}
public static void write(String key, boolean value) {
SharedPreferences.Editor prefsEditor = mSharedPref.edit();
prefsEditor.putBoolean(key, value);
prefsEditor.commit();
}
public static Integer read(String key, int defValue) {
return mSharedPref.getInt(key, defValue);
}
public static void write(String key, Integer value) {
SharedPreferences.Editor prefsEditor = mSharedPref.edit();
prefsEditor.putInt(key, value).commit();
}
}
Simply call SharedPref.init() on MainActivity once
SharedPref.init(getApplicationContext());
Write data
SharedPref.write(SharedPref.NAME, "XXXX");//save string in shared preference.
SharedPref.write(SharedPref.AGE, "25");//save int in shared preference.
SharedPref.write(SharedPref.IS_SELECT, true);//save boolean in shared preference.
Read Data
String name = SharedPref.read(SharedPref.NAME, null);//read string in shared preference.
String age = SharedPref.read(SharedPref.AGE, 0);//read int in shared preference.
String isSelect = SharedPref.read(SharedPref.IS_SELECT, false);//read boolean in shared preference.
Output
Name : "XXXX";
Age : "25";
IsSelect : "true";
Usually, I use something like this:
No static Context
reference, static getter/setter for each property, when required you can add memory cached value for some property to get it faster from memory instead of reading from SharedPreferences. Clear api.
public class SharedPreferencesManager {
private static final String APP_SETTINGS = "APP_SETTINGS";
// properties
private static final String SOME_STRING_VALUE = "SOME_STRING_VALUE";
// other properties...
private SharedPreferencesManager() {}
private static SharedPreferences getSharedPreferences(Context context) {
return context.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
}
public static String getSomeStringValue(Context context) {
return getSharedPreferences(context).getString(SOME_STRING_VALUE , null);
}
public static void setSomeStringValue(Context context, String newValue) {
final SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putString(SOME_STRING_VALUE , newValue);
editor.commit();
}
// other getters/setters
}