You can create singleton class that manage app's preferences (do not forget make your own YourAppSingleton class):
public final class PreferencesUtil {
private PreferencesUtil() {}
public static float getFloatValue(String key, float defaultValue) {
SharedPreferences preferences = getSharedPreferences();
return preferences.getFloat(key, defaultValue);
}
public static void setFloatValue(String key, float value) {
SharedPreferences preferences = getSharedPreferences();
Editor editor = preferences.edit();
editor.putFloat(key, value);
editor.commit();
}
public static long getLongValue(String key, long defaultValue) {
SharedPreferences preferences = getSharedPreferences();
return preferences.getLong(key, defaultValue);
}
public static void setLongValue(String key, long value) {
SharedPreferences preferences = getSharedPreferences();
Editor editor = preferences.edit();
editor.putLong(key, value);
editor.commit();
}
public static void setIntValue(String key, int value) {
SharedPreferences preferences = getSharedPreferences();
Editor editor = preferences.edit();
editor.putInt(key, value);
editor.commit();
}
public static int getIntValue(String key, int defaultValue) {
SharedPreferences preferences = getSharedPreferences();
return preferences.getInt(key, defaultValue);
}
public static void setStringValue(String key, String value) {
SharedPreferences preferences = getSharedPreferences();
Editor editor = preferences.edit();
editor.putString(key, value);
editor.commit();
}
public static String getStringValue(String key) {
SharedPreferences preferences = getSharedPreferences();
return preferences.getString(key, null);
}
public static String getStringValue(String key, String defValue) {
SharedPreferences preferences = getSharedPreferences();
return preferences.getString(key, defValue);
}
public static boolean getBooleanValue(String key, boolean defaultValue) {
SharedPreferences preferences = getSharedPreferences();
return preferences.getBoolean(key, defaultValue);
}
public static void setBooleanValue(String key, boolean value) {
SharedPreferences preferences = getSharedPreferences();
Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
public static void clearValue(String key) {
SharedPreferences preferences = getSharedPreferences();
Editor editor = preferences.edit();
editor.remove(key);
editor.commit();
}
private static SharedPreferences getSharedPreferences() {
return YourAppSingleton.getInstance().getSharedPreferences(YourAppSingleton.getInstance().getString(R.string.app_name), Context.MODE_PRIVATE);
}
}
Application class:
public class YourAppSingleton extends Application {
private static YourAppSingleton instance = null;
public void onCreate() {
super.onCreate();
instance = this;
}
public static YourAppSingleton getInstance() {
return instance;
}
}