Android-具有多个“共享首选项"是不好的做法吗? [英] Android - Is it bad practice to have multiple Shared Preferences?

查看:53
本文介绍了Android-具有多个“共享首选项"是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 SharedPreferences 的应用程序.一个仅存储应用程序版本以检查更新日志以检查更新日志,另一个包含一些布局信息,当用户选择时, clear()会被调用.我终于设法使 PreferenceFragment 工作并注意到了一个趋势,所以我想我现在就可以问这个,然后再去疯狂设置偏好(尽管我认为我已经足够了).

I have an app making use of SharedPreferences. One just stores app version to check against update for a changelog, the other contains some layout info that clear() gets called on as the user chooses. I finally managed to get a PreferenceFragment working and noticed a trend, so I thought I might ask this now before i go preference crazy (though I think I have enough).

我已经竭尽所能进行搜索,没有发现问题的具体提法,只是可能有多个问题.

I've done my best to search and see no specific mention of a problem, only that it's possible to have multiples.

我有点担心 PreferenceManager.getDefaultSharedPreferences()获取错误的首选项,但是我可能只是误解了用法.

I'm a little concerned about PreferenceManager.getDefaultSharedPreferences() grabbing the wrong pref, but I might simply be misunderstanding the usage.

我可以从自己的活动中想到的唯一相关代码:

The only relevant code i could think of from my activity:

SharedPreferences storedVer = getSharedPreferences(VER_NUM, 0);
SharedPreferences savedLayout = getSharedPreferences(LAYOUT_SAVE, 0);

推荐答案

这根本不是坏习惯.我认为是相反的.我认为不同的行为应使用不同的 sharedPreference 文件.

It is not bad practice at all. I think it is the opposite. I think different behaviours should use different sharedPreference files.

.getDefaultSharedPreferences()使用默认的 com.company.packagename.xml 文件.其他人创建自己的文件.

.getDefaultSharedPreferences() uses the default com.company.packagename.xml file. And the others create their own files.

我想到了使用多个 sharedPreference's 的以下优点.

The followings advantages of using multiple sharedPreference's come up in my mind.

  • 使用BackupManager时,可以提供要备份和还原的 sharedPreference 文件.
  • 用户注销后,您可以删除其中包含用户私有值的 sharedPreference 文件.您可能不想删除其他一些内容.
  • When you use BackupManager, you can provide which sharedPreference files to backup and restore.
  • When the user logout, you can delete sharedPreference file with that users private values in it. You may not want to delete some other.

这篇关于Android-具有多个“共享首选项"是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆