响应 Android 中的首选项更新
我从另一个活动调用 PreferenceActivity,然后根据首选项更改更新 onActivityResult 上的应用程序状态(即:更改字体大小)。
我想最好将状态更新逻辑放在 PreferenceActivity 中。这样我就不会在调用 PreferenceActivity 的每个活动中重复逻辑。
执行此操作的最佳或正确方法是什么?
I am calling a PreferenceActivity from another activity and then updating the application state (ie: changing the font size) on onActivityResult, based on the preference changes.
I was thinking it would be better to put the state update logic in the PreferenceActivity. That way I don't have the duplicate the logic in each activity that calls the PreferenceActivity.
What's the best or correct way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让任何关心首选项更改的 Activity(或其他组件)通过 registerOnSharedPreferenceChangeListener() 注册首选项更改侦听器。然后,当偏好以任何方式发生变化时,他们会发现并做出相应的反应。
Have any
Activity
(or other component) that cares about preference changes register a preference change listener viaregisterOnSharedPreferenceChangeListener()
. Then, when the preferences change by any means, they will find out about it and can react accordingly.PreferenceActivity 应该处理所有首选项设置。您的其他活动在运行时应该读取这些设置并相应地进行调整。
The PreferenceActivity should handle all the preference setting. Your other activities should read what those settings are when they run and adjust themselves accordingly.