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

后端 未结 12 2342
日久生厌
日久生厌 2020-12-13 08:54

How do you use SharedPreferences in a non-Activity class? I tried making a generic Preferences utility class and importing android.content.Context but Eclipse s

12条回答
  •  不思量自难忘°
    2020-12-13 09:15

    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);
    

提交回复
热议问题