Android Utililty Methods SharedPreferences Save

List of utility methods to do SharedPreferences Save

Description

The list of methods to do SharedPreferences Save are organized into topic(s).

Method

voidputPrefs(Activity context, int mode, Map pairs)
Purpose - Put preferences through the Shared Preferences API.
SharedPreferences prefs = context.getPreferences(mode);
if (pairs != null && !pairs.isEmpty()) {
    SharedPreferences.Editor editor = prefs.edit();
    for (Entry<String, String> pair : pairs.entrySet()) {
        editor.putString(pair.getKey(), pair.getValue());
    editor.commit();
voidputSharedPrefs(Context context, String prefName, int mode, Map pairs)
Purpose - Put shared preferences through the Shared Preferences API.
SharedPreferences prefs = context.getSharedPreferences(prefName,
        mode);
if (pairs != null && !pairs.isEmpty()) {
    SharedPreferences.Editor editor = prefs.edit();
    for (Entry<String, String> pair : pairs.entrySet()) {
        editor.putString(pair.getKey(), pair.getValue()).commit();
    editor.commit();
...
voidsaveSetting(Context context, String name, Long value)
save Setting
SharedPreferences prefs = context.getSharedPreferences(PREFERENCES,
        Context.MODE_PRIVATE);
prefs.edit().putLong(name, value).apply();
voidsaveSetting(Context context, String name, String value)
save Setting
SharedPreferences prefs = context.getSharedPreferences(PREFERENCES,
        Context.MODE_PRIVATE);
prefs.edit().putString(name, value).apply();
ObjectupdatePrefValueByModel(SharedPreferences pref, String key, String thisModel, String targetModel, Object targetModelValue, Object notTargetModelValue)
update Pref Value By Model
Object value;
String mdl = "#" + thisModel.toUpperCase(Locale.US) + "#";
if (targetModel != null && targetModel.indexOf(mdl) != -1) {
    value = targetModelValue;
} else {
    value = notTargetModelValue;
if (value != null) {
...
voidsetSharedPreferences(String key, String value, Context context)
set Shared Preferences
SharedPreferences prefs = PreferenceManager
        .getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
voidputPrefBoolean(Context c, String key, boolean value)
put Pref Boolean
SharedPreferences sp = PreferenceManager
        .getDefaultSharedPreferences(c);
sp.edit().putBoolean(key, value).commit();
voidputPrefInt(Context c, String key, int value)
put Pref Int
SharedPreferences sp = PreferenceManager
        .getDefaultSharedPreferences(c);
sp.edit().putInt(key, value).commit();