共享首选项问题未更新



我一直在开发android应用程序。我有两个应用程序A,B。在A中,我创建了一个文件共享偏好:

SharedPreferences sharedPref = getSharedPreferences("account", Context.MODE_WORLD_READABLE);

在B中,我从B的sharedPref.获得数据。但我有问题:
当我用新值更新A中的文件sharedPref并从A打开应用程序B以读取sharedPref。检索到的值不正确,这是旧值,它没有为应用程序B更新。我删除并重新启动应用程序B,然后它得到正确的值(新值)。你能帮我吗?非常感谢你。

editor.putString("username", user);
editor.putString("password", pass);
editor.commit();
MainActivity.this.overridePendingTransition(0, 0);
MainActivity.this.finish();
MainActivity.this.overridePendingTransition(0, 0);
Intent intent = getPackageManager().getLaunchIntentForPackage("com.inet.solution");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

替换

SharedPreferences sharedPref = getSharedPreferences("account", Context.MODE_WORLD_READABLE);

对于

SharedPreferences sharedPref = getSharedPreferences("account", MODE_PRIVATE);

希望这能帮助

你用这个

SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("account", Context.MODE_WORLD_READABLE);

试试这个链接,这对很有帮助

最新更新