Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mess with the shared preferences of android - which function to use?

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?

like image 788
Hack06 Avatar asked Dec 10 '22 16:12

Hack06


1 Answers

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... ;)

like image 108
Hack06 Avatar answered May 21 '23 09:05

Hack06