【问题标题】:Is Scope of SharedPreferences Limited to One Time Use?SharedPreferences 的范围是否仅限于一次性使用?
【发布时间】:2016-08-02 19:47:04
【问题描述】:

我对 sharedPreferences 很好奇,我真正的问题是Variable stored using sharedPreferences in one activity can be fetched in second activity but can i get the value of variable again in third activity ? does it provide this usage? if not How can i achieve this task?

【问题讨论】:

  • 你不能在第三个活动中得到它,就像你在第二个活动中得到它一样吗?您能否编辑您的问题以提供您尝试过的一些代码?
  • 你试过了吗?按照现在的表述方式,您没有理由无法在应用程序的任何地方获取变量的值。
  • 哇,首先是概念问题,而不是编码问题,所以没有示例,

标签: android sharedpreferences


【解决方案1】:

先生。 Z,

你可以在它对应的activity中存储一个SharedPreferences的静态引用(当activity的onCreate被调用时,实例化这个引用)。否则,对于在程序中全局检索数据,我唯一能想到的就是从您创建的 scard 存储中读取文件。

如果有不清楚或不完整的地方,请发表评论,我会尽力提供帮助!

【讨论】:

    【解决方案2】:

    您可以多次使用它,除非您通过代码明确清除它或您应用的本地数据已被删除(例如卸载或清除本地存储时)。

    请注意,不应使用 SharedPreferences 来存储重要信息 as it might get lost occasionally

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      相关资源
      最近更新 更多