public class PreferenceUtil {
public static void saveToPreferences(Context context, String preferenceName, String preferenceValue) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(preferenceName, preferenceValue);
editor.apply();
}
public static void saveToPreferences(Context context, String preferenceName, boolean preferenceValue) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(preferenceName, preferenceValue);
editor.apply();
}
public static String readFromPreferences(Context context, String preferenceName, String defaultValue) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
return sharedPreferences.getString(preferenceName, defaultValue);
}
public static boolean readFromPreferences(Context context, String preferenceName, boolean defaultValue) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
return sharedPreferences.getBoolean(preferenceName, defaultValue);
}
}
// Use this class to write preference and read preference
// Create instance of PreferenceUtil class in your Activity
private PreferenceUtil mPreferenceUtil;
mPreferenceUtil.saveToPreferences(contex, prefKey, prefValue);