我的应用程序中有 2 个进程。从一个过程中,我将数据保存到 SharedPreferences
。
从第二个过程 - 检索。当我检索数据时,我收到带有旧数据的 SharedPreferences
(我检查 xml 文件并查看,文件中的当前数据和接收到的数据不同)。看起来这个数据被缓存了。我更改了保存方法(提交/应用)但没有结果。
PS:仅举个例子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);
...}
最佳答案
解决方案是在打开共享首选项时添加必要的标志 Context.MODE_MULTI_PROCESS 标志(在 API 级别 11 及更高版本中可用)
关于android - 共享首选项中的数据缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8714793/