How to save and retrieve Date in SharedPreferences

To save and load accurate date, you could use the long (number) representation of a Date object. Example: //getting the current time in milliseconds, and creating a Date object from it: Date date = new Date(System.currentTimeMillis()); //or simply new Date(); //converting it back to a milliseconds representation: long millis = date.getTime(); You can use this … Read more

How to detect if changes were made in the preferences?

Do SharedPreferences.OnSharedPreferenceChangeListener spChanged = new SharedPreferences.OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { // your stuff here } }; In your PreferenceActivity, ie make it a member of your PreferenceActivity class and then do registerOnSharedPreferenceChangeListener(spChanged) in the PreferenceActivity.onCreate() method. That’s what I do and I never have a problem. Else it’s your conditional … Read more

Shared Preferences reset data when app is force closed or device is restarted

I have a login screen and wanted the app to appear as if it’s remained “logged in” at the internal screen after the app is closed/destroyed/phone call/etc. I have a Preferences Object to save values following Login or Register. I read preference values in all the key screen onResume() methods. After login (for example): SharedPreferences … Read more

How to save List to SharedPreferences?

It only possible to use primitive types because preference keep in memory. But what you can use is serialize your types with Gson into json and put string into preferences: private static SharedPreferences sharedPreferences = context.getSharedPreferences(STORE_FILE_NAME, Context.MODE_PRIVATE); private static SharedPreferences.Editor editor = sharedPreferences.edit(); public <T> void setList(String key, List<T> list) { Gson gson = new … Read more

Should accessing SharedPreferences be done off the UI Thread?

I’m glad you’re already playing with it! Some things to note: (in lazy bullet form) if this is the worst of your problems, your app’s probably in a good spot. 🙂 Writes are generally slower than reads, though, so be sure you’re using SharedPreferenced$Editor.apply() instead of commit(). apply() is new in GB and async (but … Read more

How to use SharedPreferences to save more than one values?

You can save multiple favorites in a single preference by adding numerous favorites in a single string, each favorite item separated by comma. Then you can use convertStringToArray method to convert it into String Array. Here is the full source code. Use MyUtility Methods to save multiple favorite items. MyUtility.addFavoriteItem(this, “Sports”); MyUtility.addFavoriteItem(this, “Entertainment”); get String … Read more

How to listen for preference changes within a PreferenceFragment?

I believe you just need to register/unregister the Listener in your PreferenceFragment and it will work. @Override public void onResume() { super.onResume(); getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); } @Override public void onPause() { getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); super.onPause(); } Depending on what you want to do you may not need to use a listener. Changes to the preferences are committed to SharedPreferences … Read more