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
In Kotlin you can do this:
val myClass = MyClass(this)
Now this is how you can get context in class
class MyClass(context: Context) {
val context: Context = context
}
Now to get Shared Preferences with context you can do this:
context.getSharedPreferences(...)
First, declare a private attribute in your class. In my case, I have a BaseAdapter class:
private final Context ctx;
private final Vector<Acto> actos;
public ActoAdaptador(Context ctx, Vector<Acto> as) {
super();
this.ctx = ctx;
this.actos = as;
}
Then, when to use the SharedPreferences:
ctx.getSharedPreferences("com.joninazio.euskofest.UsingPreferences_preferences", Context.MODE_PRIVATE)
That way works for me at least :)
The solution i found to this was:
1-In an MainActivity class (i.e always launched before getting any context in project) create a static variable for the context:
public static Context contextOfApplication;
2-In an important method of this class (Such as onCreate, the constructor, etc) initialize this variable using the getApplicationContext method:
public void onCreate() {
contextOfApplication = getApplicationContext();
}
3-In the same class Create a "getter" method for this variable (it must also be static):
public static Context getContextOfApplication(){
return contextOfApplication;
}
4-In the non-activity class get the context by calling the created method statically:
Context applicationContext = MyActivityClass.getContextOfApplication()
;
5-Use the PreferenceManager Class to get the SharedPreferences variable:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);
Hope it helps.
use this code to a new class.
import android.content.Context;
import android.content.SharedPreferences;
/**
* Created by Atiar Talukdar on 6/5/2017.
*/
public class TemporaryStorageSharedPreference {
protected final static int DEFAULT = 0;
int temp = 0;
public int readSharedPreference(Context context, String spName,String key){
SharedPreferences sharedPreferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
return temp = sharedPreferences.getInt(key,DEFAULT);
}
public void writeSharedPreference(Context context,String ammount,String spName,String key ){
SharedPreferences sharedPreferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, DEFAULT);
editor.commit();
}
}
create SharedPreferences class
/**
* @param mContext
* @param key
* @param value
*/
public static void savePreferences(Context mContext, String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value).apply();
}
/**
* @param context
* @param keyValue
* @return
*/
public static String getPreferences(Context context, String keyValue) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
return sharedPreferences.getString(keyValue, "");
}
/**
* @param mContext
*/
public static void removeAllSharedPreferences(Context mContext) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear().apply();
}
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);