Android Shared Preferences class in Java language + Single UI using some of its methods + picture of it's actual path
[1] Put SharedPrefs class in your app folder
[2] Use the following in the place you want to use SharedPrefs; substitute the i/p parameters by their equivalent in your project
// Making an instance of the module
SharedPrefs.init(getContenxt())
/* Used them incase you want to track changes in SharedPrefs */
// MyPrefsListener myPrefsListener = new MyPrefsListener();
// SharedPrefs.getSharedPreferences().registerOnSharedPreferenceChangeListener(myPrefsListener);
/* *************************************************** */
// Writing in sharedPrefs
SharedPrefs.write(key,value);
// Reading from sharedPrefs
SharedPrefs.readMap(key,value);
// Deleting from sharedPrefs
SharedPrefs.remove(key);
[3] Add the following inner class in any Activity/Fragment you want to track a SharedPrefs' key changes in it
private class MyPrefsListener implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(keyYouWantToTrackItsChanges)) {
if(SharedPrefs.readMap(key, defaultValue)) { /* Do specific Operation */ }
else { /* Do Another Operation */ }
}
}
}
[4] Check this example on Tracking changes In SharedPrefs class