Skip to content

Instantly share code, notes, and snippets.

@vijaybheda
Created February 8, 2020 12:57
Show Gist options
  • Select an option

  • Save vijaybheda/91491b756dc6bd34bcb2a3cfad2f8ba2 to your computer and use it in GitHub Desktop.

Select an option

Save vijaybheda/91491b756dc6bd34bcb2a3cfad2f8ba2 to your computer and use it in GitHub Desktop.
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