简体   繁体   English

设置活动应使用哪个:首选项api或sharedpreferences?

[英]Which to use for settings activity : preferences api or sharedpreferences?

I have a settings activity for users in my application. 我的应用程序中有一个针对用户的设置activity I manually add the changes to a sharedpreferences file ( simple delete , edit , add operations ). 我将更改手动添加到sharedpreferences文件(简单的delete,edit和add操作)。 It works fine but just now i heard of preferences activity . 它工作正常,但刚才我听说了偏好activity Should i use it instead? 我应该改用它吗?

PreferenceActivity and Fragment provide some advantages: PreferenceActivityFragment提供了一些优点:

  • Declare your UI through XML, where you can put preferences in categories, this makes your settings look and feel consistent with the system settings. 通过XML声明UI,您可以在其中将首选项放在类别中,这使您的设置在外观上与系统设置保持一致。
  • Declaring preferences through xml associate each preference with a key and/or a default value, this saves you from writing the logic that handles such cases. 通过xml声明首选项会将每个首选项与一个键和/或一个默认值相关联,这使您不必编写处理此类情况的逻辑。

In the end you can achieve the same results with both approaches, but I recommend using the Preferences Activity/Fragment for the consistency with the system and avoiding boilerplate code or bugs that may appear from implementing the preferences yourself. 最后,您可以通过两种方法获得相同的结果,但是我建议使用“ Preferences Activity/Fragment ”以与系统保持一致,并避免样板代码或自己实现首选项可能出现的错误。

Check these tutorials for more info: http://android-pro.blogspot.com.eg/search/label/SharedPreferences 查看这些教程以获取更多信息: http : //android-pro.blogspot.com.eg/search/label/SharedPreferences

https://developer.android.com/reference/android/preference/PreferenceFragment.html https://developer.android.com/reference/android/preference/PreferenceFragment.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM