Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharedPreferences are sometimes deleted

Tags:

My app uses SharedPreferences (with the help of a PreferenceActivity) to store all settings. But sometimes all settings are deleted and all values are set back to default. Mostly on Android 3.x tabs. On normal smartphones there are no problems.

I have observed that all settings are deleted when the application process is killed by Android. I noticed this because the notification icon is no longer displayed. I also wonder why the process is killed on tablets and not on smartphones. On my smartphone the notification icon never disappears.

My app also has a service, the main process and the service both read and write settings to the SharedPreferences. I've sometimes noticed that all settings are cleared once the service writes to the SharedPreferences. But it does not happen every time.

Any ideas? I realize that the SharedPreferences should not be used by different processes, but it works on all smartphones without problems!

like image 446
almisoft Avatar asked Sep 15 '11 22:09

almisoft


People also ask

Can shared preferences be deleted?

SharedPreferences is what Android and iOS apps use to store simple data in an allocated space. This data exists even when the app is shut down and starts up again; we can still retrieve the value as it was. The data stored in SharedPreferences can be edited and deleted.

Does SharedPreferences persist after uninstall?

I was storing a String value in SharedPreferences that was setting the value for a Dropdownmenu . Now I have updated those values so the previous are no longer valid. However, even after uninstalling the app and clearing the data the old shared preference value is still there.

Are shared preferences persistent?

Shared preferences is an Android class that allows apps to store key-value pairs of primitive data types. Once saved, information in shared preferences will persist across sessions.

Where are SharedPreferences stored?

Android Shared Preferences Overview 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.


2 Answers

I found out the reason why the settings disappear: If two processes write data to SharedPreferences, it can happen that all SharedPreferences are reset to default values. In my case all settings are cleared when the service write the first data to SharedPreferences.

Now, the service provides only read access to the SharedPreferences. The settings that are accessed by both processes writing are stored in a database.

like image 170
almisoft Avatar answered Sep 19 '22 18:09

almisoft


SharedPreferences usually work great and as you mentioned without any problem on smartphones. It should also work on tablets. However it would be best to use sqlite or backing your shared prefs on a server for each users id data loss is a problem.

like image 34
Murat Avatar answered Sep 18 '22 18:09

Murat