I have some information stored as SharedPreferences. I need to access that information from outsite an Activity (in from a domain model class). So I created a static method in an Activity which I only use to get the shared preferences.
This is giving me some problems, since apparently it is not possible to call the method "getSharedPreferences" from a static method.
Here's the message eclipse is giving me:
Cannot make a static reference to the non-static method
getSharedPreferences(String, int) from the type ContextWrapper
I tried to work around this by using an Activity instance, like this:
public static SharedPreferences getSharedPreferences () {
Activity act = new Activity();
return act.getSharedPreferences("FILE", 0);
}
This code gives a null point exception.
Is there a work-around? Am I going into an android-code-smell by trying to do this?
Thanks in advance.
Cristian's answer is good, but if you want to be able to access your shared preferences from everywhere the right way would be:
Application
, e.g.public class MyApp extends Application {
...android:name
attribute of your<application>
tag in the AndroidManifest.xml to point to your new class, e.g.android:name="MyApp"
(so the class is recognized by Android)this
) to a static field namedapp
and create a static method that returns this field, e.g.getApp()
. You then can use this method later to get a context of your application and therefore get your shared preferences. :-)I had a similar problem and I solved it by simply passing the current context to the static function:
Since you are calling from outside of an activity, you'll need to save the context:
And inside OnCreate:
Storing the context as a static variable, can cause problems because when the class is destroyed so are the static variables. This sometimes happens when the app is interrupted and becomes low on memory. Just make sure that the context is always set before you attempt to use it even when the class setting the context is randomly destroyed.
I had the same need - some of my preferences need to be accessed often, and efficiently. I also imagine that reading and writing a string from SharedPreferences is slightly slower than getting and setting a static variable (but likely to an insignificant degree). I also just kind of got used to using static fields, retrieving Preference values only at startup, and saving them on close.
I didn't love my options for keeping static references to the SharedPreferences/contexts directly, but so far this workaround has sufficed.
My solution:
Create a Settings class with all the static variables you need.
When the application initializes, retrieve SharedPreferences fields and immediately set all Settings fields (I call a "loadSharedPrefs()" method at the end of MainActivity's onCreate method).
In the SettingsActivity's preferenceChangeListener's initialization, set the appropriate static field in the Settings class. (I call a "setAppropriateSetting(key, value)" method at the beginning of SettingsActivity's onPreferenceChange()).
Use your static preferences wherever, whenever!
That's because in this case,
act
is an object that you just create. You have to let Android do that for you;getSharedPreferences()
is a method ofContext
, (Activity
,Service
and other classes extends fromContext
). So, you have to make your choice:If the method is inside an activity or other kind of context:
If the method is outside an activity or other kind of context:
Here's a better alternative to storing your shared preferences in static fields.
e.g
usage in your activity