Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample of implementing a class for managing SharedPreferences
import android.content.Context;
import android.content.SharedPreferences;
public class ApplicationData {
private static final String APP_KEY = "android_course_key";
// save a String value by key
public static void setStringValueInSharedPreferences(Context context, String key, String value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(ApplicationData.APP_KEY,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.apply();
}
// get a String value by key
public static String getStringValueFromSharedPreferences(Context context, String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(ApplicationData.APP_KEY,
Context.MODE_PRIVATE);
return sharedPreferences.getString(key, "");
}
// save an int value by key
public static void setIntValueInSharedPreferences(Context context, String key, int value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(ApplicationData.APP_KEY,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.apply();
}
// get an int value by key
public static int getIntValueFromSharedPreferences(Context context, String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(ApplicationData.APP_KEY,
Context.MODE_PRIVATE);
return sharedPreferences.getInt(key, 0);
}
// delete a value by key
public static void deleteValueFromSharedPreferences(Context context, String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(ApplicationData.APP_KEY,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(key);
editor.apply();
}
// delete all values
public static void deleteAllValuesFromSharedPreferences(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(ApplicationData.APP_KEY,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment