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", "");
Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment. getDataDirectory() .
DataStore is a new and improved data storage solution aimed at replacing SharedPreferences. Built on Kotlin coroutines and Flow, DataStore provides two different implementations: Proto DataStore, that stores typed objects (backed by protocol buffers) and Preferences DataStore, that stores key-value pairs.
there is no limit in Shared Preference. Save this answer.
Yes, it is deprecated. Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings.
I know, I know, I will get inflamed, and casted into the embers of hell....
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());
The reason I risk down-vote, is, 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.
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.
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