mobimedia mobimedia - 1 year ago 108
Android Question

Shared preferences with different context

What is the difference between these two:

SharedPreferences prefs = getDefaultSharedPreferences(Application context);


SharedPreferences prefs = getDefaultSharedPreferences(Activity context);

I am confused which one to use ?i want to make prefs static so that i can use it everywhere in my it ok?

Answer Source

In this case it shouldn't matter.

If you look at the source code then you'll see that the context you pass in is used to create a name for your preferences. This is done by calling context.getPackageName(). So as long as both contexts return the same package name (and it should be the same for any context within your app) there is no difference between using those two.