Skip to content

Instantly share code, notes, and snippets.

@lopspower
Last active July 7, 2018 22:47
Show Gist options
  • Save lopspower/dec2592872f8df65a26c to your computer and use it in GitHub Desktop.
Save lopspower/dec2592872f8df65a26c to your computer and use it in GitHub Desktop.
Utils class for the SharedPreferences management
import android.content.Context;
import android.content.SharedPreferences;
/**
* Copyright (C) 2016 Mikhael LOPEZ
* Licensed under the Apache License Version 2.0
* Utility class for the SharedPreferences management
*/
public class SharedPreferencesUtils {
// PUBLIC PREF NAME
public static final String PREFS_EXAMPLE = "example";
//region Singleton Shared Preferences
private static final String PREFS_FILE_NAME = "PrefsFile";
private static SharedPreferences mSharedPreferences;
private static SharedPreferences getSharedPreferencesEditor(Context context) {
if (mSharedPreferences == null) {
mSharedPreferences = context.getSharedPreferences(PREFS_FILE_NAME, Context.MODE_PRIVATE);
}
return mSharedPreferences;
}
//endregion
public static void setString(Context context, String name, String value) {
SharedPreferences.Editor editor = getSharedPreferencesEditor(context).edit();
editor.putString(name, value);
editor.commit();
}
public static String getString(Context context, String name) {
return getSharedPreferencesEditor(context).getString(name, null);
}
public static void remove(Context context, String name) {
getSharedPreferencesEditor(context).edit().remove(name).commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment