Reputation: 14845
Is there a way to make SharedPreferences global throughout my whole app? Right now I'm using these lines in a lot of places throughout my code to store simple on/off settings for a number of preferences that my users can set. I just want to call them once globally if possible:
SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
Any tips on how to be able to call just these line in all classes would be awesome:
editor.putString("examplesetting", "off");
editor.commit();
and
String checkedSetting = settings.getString("examplesetting", "");
Upvotes: 14
Views: 19370
Reputation: 34598
Use a singleton class that wraps around the SharedPreference
settings.. something like this:
public class PrefSingleton{
private static PrefSingleton mInstance;
private Context mContext;
//
private SharedPreferences mMyPreferences;
private PrefSingleton(){ }
public static PrefSingleton getInstance(){
if (mInstance == null) mInstance = new PrefSingleton();
return mInstance;
}
public void Initialize(Context ctxt){
mContext = ctxt;
//
mMyPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
}
}
And create wrapper functions around what your examples represented in the question, for example,
PrefSingleton.getInstance().writePreference("exampleSetting", "off");
and the implementation could be something like this:
// Within Singleton class
public void writePreference(String key, String value){
Editor e = mMyPreference.edit();
e.putString(key, value);
e.commit();
}
From your first activity, activate the singleton class, in this manner, something like this:
PrefSingleton.getInstance().Initialize(getApplicationContext());
Using global static classes can be a bad idea and goes against the practice of programming fundamentals. BUT having said that, as nit-picky aside, it will ensure only the one and only object of the class PrefSingleton
can exist and be accessible regardless of what activities the code is at.
Upvotes: 19
Reputation:
I would do this following :
public class BaseActivity extends Activity {
protected boolean setInHistory;
protected SharedPreferences sharedPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
setInHistory = true;
}
}
public class MainActivity extends BaseActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println(setInHistory+" <-- setInhistory");
}
}
Then can access sharedPrefs because it is protected then it's accessible through the package.
In the console you have : true <-- setInhistory
Upvotes: 3
Reputation: 5739
I would extend Application
and include the SharedPreferences.Editor
as a field with a getter.
public class APP extends Application {
private final SharedPreferences settings = getSharedPreferences("prefs", 0);
private final SharedPreferences.Editor editor = settings.edit();
public SharedPreferences.Editor editSharePrefs() {
return editor;
}
}
Then you can access it from any Activity
with
((APP) getApplication()).editSharePrefs().putString("examplesetting", "off");
((APP) getApplication()).editsharePrefs().commit();
Alternatively, you could also throw in the method
public static APP getAPP(Context context) {
return (APP) context.getApplicationContext();
}
Although, this would simply change the calls you make to
APP.getAPP(this).editSharePrefs().putString("examplesetting", "off");
APP.getAPP(this).editsharePrefs().commit();
So it really is a personal preference, which looks cleaner to you.
Upvotes: 11
Reputation: 46
Use a Helper class to get all you want,make the methods static.This is how ADW do.
Upvotes: 0