Imri Persiado
Imri Persiado

Reputation: 1889

SharedPreferences on a non-Activity extended class

I have a class that sends some info to the mysql db. I want from this class to use the Shared Preferences methods but they are belong to the activity class. My class extends AsyncTask so I can't extend another.. I tried to create an activity instance and use it but my program has stoped. something like this:

Activity a1 = new Activity();
SharedPreferenecs loginInfo = a1.getSharedPreferences("MyKid", 0); 

etc ..

Upvotes: 0

Views: 689

Answers (1)

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

pass the Context of your Activity in non Activity class's Constructor.

and then use

mContext.getSharedPreferences("MyKid", 0);

Upvotes: 1

Related Questions