android - 共享首选项中的数据缓存

标签 android caching sharedpreferences

我的应用程序中有 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/

相关文章:

javascript - ios/android webview 中的 Localstorage 持久化

android - 仅在 android 中静音正在运行的应用程序声音

Android Espresso 测试(用 Kotlin 编写)看不到主要类和包

apache - 如何使 mod_cache 与动态内容一起正常工作?

ios - 我的 iOS Live 应用程序。正在缓存下载链接

android - 如何在 Android 9+ 中以编程方式接听或拒绝来电?

c++ - LRU 缓存和多线程

android - 服务——为共享首选项声明静态字符串的最佳位置

android - 从服务访问共享首选项

java - 从 sharedpreference 中删除所有数据