How do I create a class specifically for the purpose of using SharedPreferences?
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.
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()
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.
精彩评论