If you have landed here, you have been exposed to the hardship of keeping the Shared Preferences of Android inline. This article will demonstrate a practical method of abstracting all the possible read and write methods for Shared Preference into a simple generic class.
Background
Basically, Shared Preference is a XML file which is acting as a small storage mean for the user’s preferences(settings). while developing an Android application, there are plenty of features or settings which are tightly bounded to the user’s taste, like the colors, themes,fonts,ringtones and etc therefore these preferences need to be saved somewhere to be retrieved on the next execution of the application and that is when the need for a small storage comes to the surface. Android has offered a simple mechanism to achieve the storage of the user’s preferences by introducing the Shared Preferences library.
A simple storage of a setting like the background color goes like this
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(applicationContext);
SharedPreferences.Editor editor = sp.edit();
editor.putString("background_color", "green");
editor.apply();
Then Android will save it in /data/data/YOUR_APP_PACKAGE_NAME/shared_prefs/YOUR_APP_PACKAGE_NAME_preferences.xml like this
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="background_color">green</string>
</map>
and later the value of the “background_color” can be retrieved from the Shared Preferences like this
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(applicationContext);
String defaultValue="white";
String Value = sp.getString("background_color", defaultValue);
Issue
As it can be seen above for storing the string value in the shared preferences, the putString method needs to be called as well as getString method for retrieving it. Seems pretty straight forward till the application begins to grow and the number of the preferences which it requires to store increase with it.
On the other hand, for storing Boolean, putBooean()/getBoolean() needs to be called innorder to store and retrieve of the preferences respectively.
Solution
The first solution which will pop up is to use a static helper class to take care of this operation, therefore for retrieving/storing String values from and to shared preferences something like the code below will be required
private static String getPrefString(Context context,String prefName,String defaultValue){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
String ret = sp.getString(prefName, defaultValue);
return ret;
}
private static void setPrefString(Context context,String prefName,String value){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sp.edit();
editor.putString(prefName, value);
editor.apply();
}
Furthermore for Boolean, StringSet, Float, Long and Integer, two methods each will be required as well (totally 12 methods) in order to be able to handle the storage/retrieval of all the possible types which one can call it is messy and unnecessary.
Another solution is to make the use of Generics like the code below
And it can be consumed for storing data as follows
new PrefManager<String>(applicationContext).set("background_color","green");
and for retrieving it as follows
new PrefManager<String>(applicationContext).get("background_color","white");
As it can be seen with only two methods all the possible data types are being handled moreover the usage is clean,practical and self explanatory. I hope you have found this way of storing/retrieving data from/to shared preferences useful as much as I did :)