【问题标题】:Android: Updating SharedPrefereces on activity resume doesn't workAndroid:更新活动简历上的 SharedPrefereces 不起作用
【发布时间】:2011-09-11 17:17:37
【问题描述】:

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

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

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

我还尝试重新实例化 SharedPrefs-Object(使用 getSharedPreferences()),但也无济于事。

提前致谢!

【问题讨论】:

  • 是的,它肯定是写入SharedPref,因为新的值会显示出来,但只有在Activity/App重启之后。
  • hmmm getSharedPreferences() 仅在 onCreate registerOnSharedPreferenceChangeListener 和 onDestroy 中取消注册并在 Listener 中进行更改?
  • 你在onRestart方法中写过吗...如果是的话希望它可以工作...
  • 嗯,我发现,如果我在活动 B 中从 onBackPressed() 而不是 onDestroy() 提交对 SharedPrefs 的更改,它就可以正常工作!

标签: android android-activity sharedpreferences onresume


【解决方案1】:

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

例如:

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

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

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

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

【讨论】:

  • 此外,Android 的一些实现会经历两次暂停/恢复周期,第二个周期在任何更改提交之前开始。当应用程序在运行 8.0.0 的三星上运行良好时,我花了 试图调试要保存的标志失败(我使用的是异步的 apply()。) Moto G 运行 5.1。对于应用范围内的重要内容,始终提交,从不申请。
【解决方案2】:

SharedPreferences 不用于在活动之间共享数据

使用IntentActivity.startActivityForResult。在这里查看我的答案 Get the intent object in an activity

【讨论】:

  • 我知道,但我需要将这些值放在 SharedPrefs 中,以便应用程序中的每个活动都可以查询它们
【解决方案3】:

确保您在每个活动中使用相同的首选项:如果您使用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);

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

【讨论】:

    【解决方案4】:

    还值得注意的是,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();
    

    【讨论】:

      【解决方案5】:

      要在从 B 恢复活动 A 时,使用从活动 B 发送到 SharedPreferences 的数据来更新活动 A,请执行以下操作:

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

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

        Intent 意图 = new Intent(activityB.this, activityA.class); 意图.addFlags(意图.FLAG_ACTIVITY_CLEAR_TOP); 开始活动(意图); 完成();

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

      希望这会有所帮助...

      【讨论】:

        猜你喜欢
        • 2013-03-02
        • 1970-01-01
        • 2022-01-12
        • 2018-02-28
        • 2014-06-18
        • 1970-01-01
        • 2012-02-25
        • 2015-02-20
        • 1970-01-01
        相关资源
        最近更新 更多