Просто просто, не нужно больше работать, используя класс утилит Preference в вашем приложении.
public class PrefUtils {
//preference file
private static final String DEFAULT_PREFS = "GIVE YOUR APP NAME HERE";
//any numeric getter method will return -1 as default value
private static final int DEFAULT_NUMERIC_VALUE = -1;
//any string getter method will return empty string as default value
private static final String DEFAULT_STRING_VALUE = "";
public static void setString(Context mContext,String key, @Nullable String value) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString(key, value);
editor.apply();
}
public static String getString(Context mContext,String key) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
return prefs.getString(key, DEFAULT_STRING_VALUE);
}
public static void setBoolean(Context mContext,String key, boolean value) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putBoolean(key, value);
editor.apply();
}
public static boolean getBoolean(Context mContext,String key) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
return prefs.getBoolean(key, false);
}
public static void setLong(Context mContext,String key, long value) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putLong(key, value);
editor.apply();
}
public static long getLong(Context mContext,String key) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
return prefs.getLong(key, DEFAULT_NUMERIC_VALUE);
}
public static void setInteger(Context mContext,String key, int value) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt(key, value);
editor.apply();
}
public static int getInteger(Context mContext,String key) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
return prefs.getInt(key, DEFAULT_NUMERIC_VALUE);
}
public static void setFloat(Context mContext,String key, float value) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putFloat(key, value);
editor.apply();
}
public static float getFloat(Context mContext,String key) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
return prefs.getFloat(key, (float) DEFAULT_NUMERIC_VALUE);
}
public static void setObject(Context mContext,String key, @NonNull Object value) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString(key, mGson.toJson(value));
editor.apply();
}
@Nullable
public static <T> T getObject(Context mContext,String key, Class<T> pojoClass) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
String jsonString = prefs.getString(key, null);
if (jsonString == null) {
return null;
}
return mGson.fromJson(jsonString, pojoClass);
}
public static Map<String, ?> getAll(Context mContext) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
return prefs.getAll();
}
public static void removeKey(Context mContext,String key) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.remove(key);
editor.apply();
}
public static void clearAll(Context mContext) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.clear();
editor.apply();
}
public static boolean setPreferenceArray(Context mContext, String key,
ArrayList<String> array) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(key + "_size", array.size());
for (int i = 0; i < array.size(); i++)
editor.putString(key + "_" + i, array.get(i));
return editor.commit();
}
public static ArrayList<String> getPreferenceArray(Context mContext,String key) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
int size = prefs.getInt(key + "_size", 0);
ArrayList<String> array = new ArrayList<>(size);
for (int i = 0; i < size; i++)
array.add(prefs.getString(key + "_" + i, null));
return array;
}
}
Ex. Вы должны сделать следующие вещи, чтобы использовать выше класс.
(1) Сохранить значение в предпочтение, как.
PrefUtils.setString(context,"Key","Value you need to store");
(2) Извлечь значение из предпочтения.
PrefUtils.getString(context,"Key");