恢复时重新加载 PreferenceActivity 中的首选项
在我的应用中,当 PreferenceActivity
未打开时,某些设置可能会发生更改,而我遇到的问题是在 onCreate< 中调用
addPreferencesFromResource
/code>,也就是说,我打开 PreferenceActivity
,然后从那里转到另一个屏幕,然后执行一些更改设置的操作,然后按返回键返回到 PreferenceActivity< /code>,然后确定布局上的设置未更改。
那么,如何在每次调用 onResume
(或 onStart()
)时重新加载所有 Preferences
而不重复布局?
In my app, some settings can possibly be changed while the PreferenceActivity
is not open, and an issue I'm running into is that addPreferencesFromResource
is called in onCreate
, so say, I open the PreferenceActivity
, then go to another screen from there, then do something that changes the settings, then hit the back key to go back to the PreferenceActivity
, then certain settings have not changed on the layout.
So, how could I re-load all the Preferences
every time onResume
(or onStart()
) is called without duplicating the layout?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:此解决方案仅适用于 API 11 +。
我不确定我是否完全理解您的问题,但您可以在活动的 onResume 中添加对 recreate() 的调用,根据我的理解,该活动可以再次经历整个生命周期。
为了确保您只在确实存在脏数据时才执行此操作,我会在 SharedPreferences 中设置一个标志,让您的活动在 onResume() 中知道它需要重新创建。
edit: This solution will work for API 11 + only.
Im not sure I fully understand your problem, but you could add a call to recreate() into the onResume of the activity which from my understanding has the activity go through the entire lifecycle again.
In order to make sure that you only do this when there is in fact dirty data, I would set a flag in the SharedPreferences that lets your activity know in the onResume() that it needs to be recreated.
我有类似的问题。由于无法找到一种简单的方法让我的 PreferenceActivity 自行刷新,我的解决方案是将其添加到我的 PreferenceActivity 中:
这将导致 Prefs 屏幕在下次启动时从 SharedPreferences 重新加载。不用说,如果您希望能够使用后退按钮返回到首选项屏幕,则此方法将不起作用。
I had a similar problem. Failing to find a simple way to make my PreferenceActivity refresh itself, my solution was to add this to my PreferenceActivity:
This will cause the Prefs screen to reload from SharedPreferences next time it is started. Needless to say, this approach won't work if you want to be able to go back to your preferences screen by using the back button.