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.