views:

24

answers:

2

I have a couple activities in my app that I would like to utilize shared preferences. Initially, I created a method in each activity to utilize SharedPreferences, which worked fine. However, since there are multiple activities that use the same data, I’m basically tucking similar methods in multiple places. So it seemed like it made more sense to create a class specifically for the purpose of handling all these methods.

Unfortunately, I don’t understand how to do it properly.

This won’t compile, because it says “getSharedPreferences is undefined for the type AppPrefs.”

public class AppPrefs {
 public void foo() {
  SharedPreferences settings = getSharedPreferences("MyAppPrefs", 0);
 } 
}

Finally, I thought, maybe since SharedPreferences is an interface I could do this, but then I’d have to implement the inherited methods. I have no reason to Override any of those methods, so there is no reason to do this either.

public class AppPrefs implements SharedPreferences {
 public void foo() {
  SharedPreferences settings = getSharedPreferences("MyAppPrefs", 0);
 } 
}

What makes sense to do here? Is there a concept am I missing? Could anyone elaborate and explain? Thanks.

+1  A: 

If the preferences are global to the application, you can use PreferenceManager.getDefaultSharedPreferences(); when you need to access the common preferences. If the preferences are specific to a subset of Activities, you have a few different options:

You can make a Activity subclass that is extended by all classes which need to access the preferences:

public abstract class AbstractFooActivity extends Activity
{
    protected SharedPreferences getFooPreferences()
    {
        return getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    }

    private static final String PREFS_NAME = "FooPrefs";
}

public class AFooActivity extends AbstractFooActivity
{
    public void aMethodThatNeedsPrefs()
    {
        // ...
        SharedPreferences myPrefs = getFooPreferences();
    }
}

Or, if like me, you'd rather not mess with the class hierarchy you can simply create a common constant value for the group of activities which need to access the preferences. This is useful in the situation where you have class outside of the Activity hierarchy that need to access the preferences. For instance, a Service.

public final class FooConstants
{
    public static final String FOO_PREFS_NAME = "FooPrefs";
}

public class AFooActivity extends Activity
{
    public void aMethodThatNeedsPrefs()
    {
        // ...
        SharedPreferences myPrefs = getSharedPreferences(FOO_PREFS_NAME, MODE_PRIVATE);
    }
}

public class AFooService extends Service
{
    public void aMethodThatNeedsPrefs()
    {
        // ...
        SharedPreferences myPrefs = getSharedPreferences(FOO_PREFS_NAME, MODE_PRIVATE);
    }
}

The second method is slightly less encapsulated, but puts fewer restrictions on the object hierarchy, which is a good tradeoff in my opinion.

codelark
So, it looks like I can't use SharedPreferences without extending Activity or implementing the interface. Seems rediculous that I need to create a sub activity just to use SharedPreferences. Good info though!
+1  A: 

Pass your context into your preference getter.

public class AppPrefs {
 public static void foo(Context ctx) {
  SharedPreferences settings = ctx.getSharedPreferences("MyAppPrefs", 0);
 } 
}

Now just pass in this from an activity class to foo()

Falmarri