Jake Wilson Jake Wilson - 1 year ago 96
Android Question

Android - How to use SharedPreferences in non-Activity class?

How do you use SharedPreferences in a non-Activity class? I tried making a generic Preferences utility class and importing

but Eclipse still wouldn't let me use

Answer Source

SharedPreferences are related to context. You can only reference it through a context.

You can simply pass context as a parameter to your class. For example in the constructor.

In your activity do:

MyClass myClass = new MyClass(this);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download