我的应用程序中有 2 个进程。从一个进程我将数据保存到SharedPreferences
。
从第二个过程 - 检索。当我检索数据时,我收到SharedPreferences
使用旧数据(我检查 xml 文件,发现当前文件中的数据和收到的数据不同)。看来该数据已被缓存。我更改了保存方法(提交/应用)但没有结果。
PS:仅举个例子http://pastebin.com/Zx2ffvSg 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 及更高版本中可用)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)