Skip to content

Instantly share code, notes, and snippets.

@Gnzlt
Last active November 30, 2016 11:18
Show Gist options
  • Save Gnzlt/211c381303879e4d895a to your computer and use it in GitHub Desktop.
Save Gnzlt/211c381303879e4d895a to your computer and use it in GitHub Desktop.
Generic Class to write/save Android preferences data using SharedPreferences
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class PrefsUtils {
private static SharedPreferences getPreferences(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}
private static SharedPreferences.Editor getPreferencesEditor(Context context) {
return getPreferences(context).edit();
}
public static String getPreferenceValue(Context context, String key, String defValue) {
return getPreferences(context).getString(key, defValue);
}
public static int getPreferenceValue(Context context, String key, int defValue) {
return getPreferences(context).getInt(key, defValue);
}
public static boolean getPreferenceValue(Context context, String key, boolean defValue) {
return getPreferences(context).getBoolean(key, defValue);
}
public static void setPreferenceValue(Context context, String key, String prefsValue) {
getPreferencesEditor(context).putString(key, prefsValue).apply();
}
public static void setPreferenceValue(Context context, String key, int prefsValue) {
getPreferencesEditor(context).putInt(key, prefsValue).apply();
}
public static void setPreferenceValue(Context context, String key, boolean prefsValue) {
getPreferencesEditor(context).putBoolean(key, prefsValue).apply();
}
public static boolean containsPreferenceKey(Context context, String key) {
return getPreferences(context).contains(key);
}
public static void removePreferenceValue(Context context, String key) {
getPreferencesEditor(context).remove(key).apply();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment