Android:在活动恢复中更新 SharedPrefereces 不起作用

发布于 2024-11-14 16:40:01 字数 586 浏览 6 评论 0原文

我有一个“活动 a”,它从 SharedPreferences 读取一些值并将它们显示在 TextView 中,然后我调用“活动 b”,其中的值来自 SharedPreferences 得到更新并写回 SharedPreferences。最后,我通过按后退按钮返回到“活动 a”,现在应从 SharedPreferences 读取新的(更新的)值并显示在 TextView 中。但问题来了,刚刚从 SharedPreferences 读取的值仍然没有更新(不是 Activity b 设置的新值)(从 logcat 输出中获取),这是怎么回事? SharedPrefs 是否需要某种手动刷新?

如果我重新启动“活动 a”,一切都会正常工作,并且新值会正确显示。怎么了?

我调用该方法来读取并显示“活动 a”中 onResume() 的值。

我还尝试重新实例化 SharedPrefs-Object (使用 getSharedPreferences() ),但它也没有帮助。

提前致谢!

I've got an "activity a" which reads some values from SharedPreferences and display them in a TextView, then I call "activity b" where the values from SharedPreferences get updated and written back to SharedPreferences. Finally I go back to "activity a" by pressing the back-button, now the new (updated) values should be read from SharedPreferences and shown in the TextView. But here comes the problem, the values just read from SharedPreferences are still not updated (are not the new ones set by activity b) (got it from logcat output), how comes that? Does SharedPrefs need some kind of manual refresh?

If I restart "activity a" everything works just fine and the new values are shown properly. What's the matter?

I call the method to read and show the values from onResume() in "activity a".

I also tried to re-instantiate the SharedPrefs-Object (with getSharedPreferences()) but it doesn't help either.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

玩套路吗 2024-11-21 16:40:01

您是否在活动 b 中调用 commit() 方法来保存新值。

例如:

SharedPreferences customSharedPreference = getSharedPreferences("abcprefs", 0);
SharedPreferences.Editor editor = customSharedPreference.edit();
editor.putString("key", "val");
editor.commit();

其次,您可以在发送到活动 b 之前完成活动 a,然后从活动 ba 创建活动 a 的新实例,并调用 onCreate() 。

或者,您可以刷新 onStart() 中的首选项,因为当发送到活动 b 时,您的活动可能“不再可见”。

请参阅http://developer.android.com/guide/topics/fundamentals/activities。 html 查看活动生命周期。

Are you calling the commit() method in activity b to save the new values.

Eg something like:

SharedPreferences customSharedPreference = getSharedPreferences("abcprefs", 0);
SharedPreferences.Editor editor = customSharedPreference.edit();
editor.putString("key", "val");
editor.commit();

And secondly you can finish() the activity a before being sent to activity b, then from activity b a new instance of activity a will be created and onCreate() will be called.

Alternatively you can refresh the preferences in the onStart() because your activity is probably "no longer visible" when sent to activity b.

See http://developer.android.com/guide/topics/fundamentals/activities.html to see the activity lifecycle.

月寒剑心 2024-11-21 16:40:01

SharedPreferences 不适用于在 Activity

Use IntentActivity.startActivityForResult 之间共享数据。在这里查看我的答案
获取 Activity 中的 Intent 对象

SharedPreferences is not for sharing data between Activities

Use Intent and Activity.startActivityForResult. See my answer here
Get the intent object in an activity

扛刀软妹 2024-11-21 16:40:01

确保您在每个活动中使用相同的首选项:如果您使用 getSharedPreferences,则应指定文件和访问级别。就您而言,听起来 getDefaultSharedPreferences 是正确的选择。

另外,请确保您不仅设置首选项,而且还提交更改:

SharedPreferences preferences = getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("key", "value");
editor.commit();

然后在其他活动中:

SharedPreferences preferences = getDefaultSharedPreferences(this);
boolean myPreference = preferences.getBoolean("key", defaultValue);

如果您发布有问题的代码片段,这会更容易提供帮助;如果您仍然无法使其正常工作,我会尝试将其添加到您的帖子中。

Ensure you're using the same preferences throughout each of your activities: if you're using getSharedPreferences, you should specify the file and level of access. In your case, it sounds like getDefaultSharedPreferences would be the way to go.

Also, make sure that you're not only setting the preferences, but also committing the changes:

SharedPreferences preferences = getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("key", "value");
editor.commit();

and then in your other activities:

SharedPreferences preferences = getDefaultSharedPreferences(this);
boolean myPreference = preferences.getBoolean("key", defaultValue);

This would be easier to help with if you would post the pieces of code in question; if you're still unable to get it working, I would try adding it to your post.

另类 2024-11-21 16:40:01

还值得注意的是,preference.edit() 每次调用它时都会返回不同的 SharedPreferences.Editor,因此将编辑器存储到单独的变量中非常重要,使用它来写出首选项,然后提交该编辑器。例如,这行不通:

myPrefs.edit().putInt("pref", 1);
myPrefs.edit().putBoolean("pref", true);
myPrefs.edit().commit();

它需要(正如已经证明的那样):

SharedPreferences myPrefs = getSharedPreferences("pref_name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.putInt("pref", 1);
editor.putBoolean("pref", true);
editor.commit();

It's also worth noting that preference.edit() returns a different SharedPreferences.Editor each time you call it, so it's important to store the editor into a separate variable, use it to write out the preferences and then commit that editor. E.g. this won't work:

myPrefs.edit().putInt("pref", 1);
myPrefs.edit().putBoolean("pref", true);
myPrefs.edit().commit();

It needs to be (as has been demonstrated):

SharedPreferences myPrefs = getSharedPreferences("pref_name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.putInt("pref", 1);
editor.putBoolean("pref", true);
editor.commit();
无边思念无边月 2024-11-21 16:40:01

为了能够使用从活动 B 发送到 SharedPreferences 的数据来更新您的活动 A,同时从 B 恢复活动 A,请执行以下操作:

  1. 在您的应用清单中,将活动 A 的“launchMode”设置为“standard”

  2. 从活动 B 完成并返回到活动 A 时,将“FLAG_ACTIVITY_CLEAR_TOP”的意图标志添加到您的意图中,例如如下:

    Intent意图 = new Intent(activityB.this, ActivityA.class);
    Intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    启动活动(意图);
    finish();

解释代码:“FLAG_ACTIVITY_CLEAR_TOP”检查正在启动的活动 A 是否已在当前任务中运行,然后不是启动该活动的新实例,而是启动该活动之上的所有其他活动销毁,并且通过 onNewIntent 方法将此 Intent 传递到 Activity 的恢复实例(现在位于顶部)。请点击此链接了解有关 Android 任务和返回堆栈的更多信息: https ://blog.mindorks.com/android-task-and-back-stack-review-5017f2c18196

希望这有帮助...

To be able to update your activity A with data sent to the SharedPreferences from activity B while resuming the activity A from B, do the following:

  1. In your app manifest, set the activity A "launchMode" to "standard"

  2. On finishing from the activity B and returning to activity A, add an intent flag of "FLAG_ACTIVITY_CLEAR_TOP" to your intent like below:

    Intent intent = new Intent(activityB.this, activityA.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    finish();

To explain the Code: The "FLAG_ACTIVITY_CLEAR_TOP" checks if activity A being started is already running in the current task, then instead of launching the new instance of that Activity, all the other activities on top of it is destroyed and this intent is delivered to the resumed instance of the Activity (now on top), through onNewIntent method. Follow this link to learn more about android task and back stack: https://blog.mindorks.com/android-task-and-back-stack-review-5017f2c18196

Hope this helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文