I want to save data using Shared Preferences in android. But I am looking to use separate class to do this task. I have implemented that class like below,
import android.content.Context;
import android.content.SharedPreferences;
public class SavePref {
private Context context;
public SavePref(Context context){
this.context = context;
}
public void saveInt(String key, int value) {
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(key, value);
editor.commit();
}
}
But there is an error on getActivity()
,
The method getActivity() is undefined for the type SavePref
How to solve this?
Thanks
getActivity()
is a method of Fragment
not of your SavePref
. In your case the simple fix is to use the Context you are keeping as member variable to retrieve the SharedPreferences
. An alternative to your approach is to avoid keeping the context as member variable, linking somehow the shared preferences to an instance of of your SavePref
class, and have a static method
public static void saveInt(Context context, String key, int value) {
SharedPreferences sharedPref = context.getDefaultSharedPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(key, value);
editor.commit();
}
and address the method like:
SavePref.saveInt(getActivity(), key, value);
from a Fragment
or
SavePref.saveInt(this, key, value);
from an Activity
. This way you don't need to instantiate SavePref every time you need to call saveInt
, and you can avoid to store a reference to the Context
.
I have created a class to do the same thing. This way you can save boolean, int, string data or check if data has been saved in a specific group of preferences for your app.
So, you need to create a class "DataProccessor.java" and put the content down bellow. Also I'm going to leave some examples about how to use it.
I hope it can helps you
import android.content.Context;
import android.content.SharedPreferences;
public class DataProccessor {
private static Context context;
public DataProccessor(Context context){
this.context = context;
}
public final static String PREFS_NAME = "appname_prefs";
public boolean sharedPreferenceExist(String key)
{
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
if(!prefs.contains(key)){
return true;
}
else {
return false;
}
}
public static void setInt( String key, int value) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME,0);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(key, value);
editor.apply();
}
public static int getInt(String key) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
return prefs.getInt(key, 0);
}
public static void setStr(String key, String value) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME,0);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.apply();
}
public static String getStr(String key) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
return prefs.getString(key,"DNF");
}
public static void setBool(String key, boolean value) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME,0);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(key, value);
editor.apply();
}
public static boolean getBool(String key) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
return prefs.getBoolean(key,false);
}
}
To use it I'll show you some small examples about how to save a String value in case you want to use it in an Activity, Fragment or Service.
For an Activity
//// To Save a value
DataProccessor dataProccessor = new DataProccessor(this);
dataProccessor.setStr("email","[email protected]");
//// To Retreive a value
dataProccessor.getStr("email");
For a Fragment
//// To Save a value
DataProccessor dataProccessor = new DataProccessor(getActivity());
dataProccessor.setStr("email","[email protected]");
//// To Retreive a value
dataProccessor.getStr("email");
For a Service
//// To Save a value
DataProccessor dataProccessor = new DataProccessor(getApplicationContext());
dataProccessor.setStr("email","[email protected]");
//// To Retreive a value
dataProccessor.getStr("email");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With