Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data cache in Shared Preference

I have 2 processes in my application. From one process i save data in to SharedPreferences. From second process - retrieve. When i retrieve data, i receive SharedPreferences with old data(i check xml file and see, that currently data in file and data that was received are different). It looks like this data was cached. I changed saving methods (commit/apply) but no result. PS: just for example http://pastebin.com/Zx2ffvSg

//saving
{ ...
 SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = myPrefs.edit();
    prefsEditor.putString(MY_NAME, "Sai");
    prefsEditor.putString(MY_WALLPAPER, "f664.PNG");
    prefsEditor.commit();
... }

//retrieving
// when i call getData() I put "this" as argument.
public void getData(Context context){
SharedPreferences myPrefs = context.getSharedPreferences("myPrefs", MODE_PRIVATE);
...}
like image 369
x90 Avatar asked Jan 03 '12 15:01

x90


People also ask

How is data stored in shared preferences?

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() .

Does clearing cache delete shared preferences?

Clearing an app's cache does not clear the settings. The settings data (including shared preferences) is stored in a different place than the app's cache. You need to "clear data" to clear out the shared preferences.

Is SharedPreferences a cache?

Android's SharedPreferences class is used in many sample apps to store small amounts of non-relational data, and avoids doing extra disk I/O with an in-memory cache. You should not use it, for a whole host of reasons: Synchronous API encourages StrictMode violations.

How much data can be stored in shared preference?

There is no limit (except for storage space), but these are currently written as a single XML file for the entire shared preference object, so you don't want to go crazy with how much you put there. If you have a large amount of data there are numerous other facilities: databases, flat files, etc.


1 Answers

The solution is add to neccesary flags Context.MODE_MULTI_PROCESS flag when open shared preference (Available in API Level 11 and up)

like image 192
x90 Avatar answered Sep 22 '22 10:09

x90