Reputation: 1092
Here's a standard task: store some values in the application's shared preferences in order to be able to retrieve it later on. But one will discover that there are 3 functions to store a value in there:
//1.
public static SharedPreferences PreferenceManager.getDefaultSharedPreferences(Context context) {}
//2.
public SharedPreferences Activity.getPreferences(int mode) {}
//3.
public SharedPreferences ContextWrapper.getSharedPreferences(String name, int mode) {}
So now here's the question: which one to choose and which one is better or there's a different purpose for each of them?
Upvotes: 4
Views: 2086
Reputation: 3584
I am sharing mine, hope it will make life easy -
public class SharedPreferenceStore {
public static void deleteValue(Context context, String key) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = preferences.edit();
editor.remove(key).apply();
}
public static void storeValue(Context context, String key, String value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = preferences.edit();
editor.putString(key, value).apply();
}
public static void storeValue(Context context, String key, boolean value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = preferences.edit();
editor.putBoolean(key, value).apply();
}
public static void storeValue(Context context, String key, double value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String doubleVal = String.valueOf(value);
Editor editor = preferences.edit();
editor.putString(key, doubleVal).apply();
}
public static void storeValue(Context context, String key, float value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = preferences.edit();
editor.putFloat(key, value).apply();
}
public static void storeValue(Context context, String key, long value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = preferences.edit();
editor.putLong(key, value).apply();
}
public static void storeValue(Context context, String key, int value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = preferences.edit();
editor.putInt(key, value).apply();
}
/*************************
* GET Methods
***************************************/
public static String getValue(Context context, String key, String defValue) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, defValue);
}
public static boolean getValue(Context context, String key, boolean defValue) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getBoolean(key, defValue);
}
public static double getValue(Context context, String key, double defValue) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String doubleVal = String.valueOf(defValue);
return Double.parseDouble(preferences.getString(key, doubleVal));
}
public static float getValue(Context context, String key, float defValue) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getFloat(key, defValue);
}
public static long getValue(Context context, String key, long defValue) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getLong(key, defValue);
}
public static int getValue(Context context, String key, int defValue) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getInt(key, defValue);
}
}
Just use the class and forget about all complications of SharedPrefences. Hope it will help you :)
Upvotes: 4
Reputation: 1092
Here's the answer to my own question:
First, let's take a look at the implementations of those 3 functions.
//1.
public static SharedPreferences PreferenceManager.getDefaultSharedPreferences(Context context) {
return context.getSharedPreferences(getDefaultSharedPreferencesName(context), getDefaultSharedPreferencesMode());
}
//2.
public SharedPreferences Activity.getPreferences(int mode) {
return getSharedPreferences(getLocalClassName(), mode);
}
//3.
public SharedPreferences ContextWrapper.getSharedPreferences(String name, int mode) {
return mBase.getSharedPreferences(name, mode);
}
Here mBase is a reference to an object of type Context.
We see that the 2nd functions calls the 3rd one, and all those 3 functions are basically equivalent but with different parameters. Think of overloading.
Next, going deeper to the 1st function's implementation, we can simplify its call as the following:
//1.
public static SharedPreferences PreferenceManager.getDefaultSharedPreferences(Context context) {
return context.getSharedPreferences(context.getPackageName() +
"_preferences", Context.MODE_PRIVATE);
}
and similarly, for the second function:
//2.
public SharedPreferences Activity.getPreferences(int mode) {
return mBase.getSharedPreferences(getLocalClassName(), mode);
}
To sum-up, the 1st function creates a shared preference file with a name as <your_package_name>_preferences
, the 2nd function creates a shared preference file with a name as <your_class_name>
, and finally, the 3rd function lets you to specify arbitrary name for the shared preference file.
Needless to say that you need to specify the correct name for the shared preference file in order to retrieve the saved values back. So you may use the 3rd function to specify the name yourself or to use the 1st or 2nd function respective to how you have saved it before.
Warning! Make sure you are passing the correct instance of the Context class. For example, a messy scenario would look like this: you are saving into shared preferences from a background thread which is running in the system (like for example when using the android's out-of-the-box SyncAdapter framework) and trying to get back the saved values from your UI-thread, you may get the default/wrong values!
Hope this will be helpful for someone else... ;)
Upvotes: 6
Reputation: 19273
its one and only method, just different calls and accesibility
SharedPreferences
work) and it will be available from every Activity
/Context
(lets say "globally") - if you want to keep ony few values you may use thisAcivity
- "private" for storing Activity
, but be aware about int mode
Upvotes: 2
Reputation: 25058
It depends. #1 Will return the SharedPreferences for whichever Context you pass it. #2 Will return the SharedPreferences for the context of the Activity you're in. This may be the same as #1 or it might not. #3 Will let you break your SharedPreferences up into different groups and name them. This might be a nice way to break things up but I have never actually done it.
Upvotes: 1