Inspired by Magenta’s book “Learning Android,” I’ve created an Application class for my app:
public class KITAppClass extends Application implements
OnSharedPreferenceChangeListener {
private static final String TAG = KITAppClass.class.getSimpleName();
//private SharedPreferences KITPrefs;
public SharedPreferences KITPrefs;
. . .
However, now I’m getting “KITPrefs cannot be resolved” on this line in a different class file that references the SharedPreferences:
allContacts.setSelected(KITPrefs.getBoolean("allContacts", false));
I’m wondering: if the prefs are shared, why were they marked “private” in the example code?
Why is it still not seen, even after I marked it “public”?
If I add “import KITAppClass;” I get virtually paddled with “The import KITAppClass cannot be resolved”
Try…
Then access
kitPrefsfrom theActivity(or other) classes in the app (assuming they occupy the same namespace) using, for example…In saying that, maintaining an instance of a
SharedPreferencesat theApplicationclass level isn’t nesessary as you can get theSharedPreferencesfrom anyActivityat any time. See Using Shared Preferences.Also, think twice about actually extending
Applicationunless you really have to and unless you really know what you’re doing with it. In the majority of cases (for simple apps at least) it isn’t necessary. Just because they show you how to do it in a book doesn’t mean you need to do it.