Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing SharedPreferences through static methods

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.

like image 659
MyName Avatar asked Sep 27 '10 17:09

MyName


People also ask

How do I access SharedPreferences?

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.

What is the method used to store and retrieve data on the SharedPreferences?

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.

Is SharedPreferences thread safe?

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.

Can we use SharedPreferences in fragment?

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


2 Answers

Cristian's answer is good, but if you want to be able to access your shared preferences from everywhere the right way would be:

  1. Create a subclass of Application, e.g. public class MyApp extends Application {...
  2. Set the 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)
  3. In the onCreate() method of your app instance, save your context (e.g. 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. :-)
like image 127
mreichelt Avatar answered Oct 12 '22 06:10

mreichelt


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()); 
like image 38
Cristian Avatar answered Oct 12 '22 07:10

Cristian