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.
These preferences will automatically save to SharedPreferences as the user interacts with them. To retrieve an instance of SharedPreferences that the preference hierarchy in this activity will use, call getDefaultSharedPreferences(android. content. Context) with a context in the same package as this activity.
Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.
The SharedPreferences implementation in Android is thread-safe but not process-safe. Normally your app will run all in the same process, but it's possible for you to configure it in the AndroidManifest. xml so, say, the service runs in a separate process than, say, the activity.
The method getSharedPreferences is a method of the Context object, so just calling getSharedPreferences from a Fragment will not work... because it is not a Context! (Activity is an extension of Context, so we can call getSharedPreferences from it).
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 named app
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. :-)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 of Context
, (Activity
, Service
and other classes extends from Context
). So, you have to make your choice:
If the method is inside an activity or other kind of context:
getApplicationContext().getSharedPreferences("foo", 0);
If the method is outside an activity or other kind of context:
// you have to pass the context to it. In your case: // this is inside a public class public static SharedPreferences getSharedPreferences (Context ctxt) { return ctxt.getSharedPreferences("FILE", 0); } // and, this is in your activity YourClass.this.getSharedPreferences(YourClass.this.getApplicationContext());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With