Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

sharedpreferences - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...