Last active
January 5, 2021 18:52
-
-
Save magdamiu/c09e5498c68e780566bda97c748cf414 to your computer and use it in GitHub Desktop.
Sample of implementing a class for managing SharedPreferences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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