如果首选项更改则刷新活动
我有一个带有应用程序设置的 PreferenceActivity,如果某些首选项发生更改,我想刷新活动。
例如,我有一个复选框负责布局中的附加选项卡,当选中复选框时必须显示选项卡。为了捕获此事件,我使用了 OnSharedPreferenceChangeListener。但是这个监听器监听这个复选框的每次点击,并且每次、每次点击都执行一些操作(你的逻辑),即使设置确实没有改变。仅当设置确实发生更改时,我才想刷新我的活动。如果之前是“OFF”,现在变成“ON”,在这种情况下我想刷新,但如果我更改为OFF - ON - OFF,我不想刷新。
如何捕捉真正的变化以及如何在活动中处理这个变化?
谢谢!
I have a PreferenceActivity with settings for my app and I want to refresh activity if some preferences was changed.
For example I have a checkbox which responsible for addtional tab in my layout, when checkbox is checked tab must be shown. To catch this event I used OnSharedPreferenceChangeListener. But this listener listen every click on this checkbox and do something (your logic) every time, every click even though setting really was not changed. I want to refresh my activity only if setting was really changed. If earlier was "OFF" and now become "ON" in this case I want to refresh, but if I changed OFF - ON - OFF I don't want to refresh.
How to catch really changes and how to handle this in activity?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
步骤#1:在您的主要 Activity 上实现
OnSharedPreferenceChangeListener
。步骤#2:维护一个
boolean
数据成员,最初为false
,只要您感兴趣的复选框发生更改,您就可以切换该成员。步骤#3:在
onStart()
或onResume()
中,如果boolean
数据成员为true
,则刷新活动并将数据成员设置为false
。Step #1: Implement the
OnSharedPreferenceChangeListener
on your main activity.Step #2: Maintain a
boolean
data member, initiallyfalse
, which you toggle whenever your checkbox of interest is changed.Step #3: In
onStart()
oronResume()
, if theboolean
data member istrue
, refresh the activity and set the data member tofalse
.