每当特定的SharedPreference值更改时重新加载片段

时间:2013-12-06 14:37:39

标签: android android-fragments listener sharedpreferences

我在主要活动中有以下片段布局:

 _______________ _______________ _______________
|               |               |               |
|               |               |               |
|     Left      |     Main      |     Right     |
|  SlidingMenu  |   Fragment    |  SlidingMenu  |
|               |               |               |
|               |               |               |
|_______________|_______________|_______________|

很多时候用户在左侧或右侧滑动菜单中更改某些内容,然后我需要在主片段或左侧或右侧SlidingMenu中重新加载数据(在大多数情况下,从SQLite数据库中填充ListViews)。我正在谈论的更改始终保存到SharedPreferences

每当某个特定SharedPreferences值发生变化时,如何重新加载数据?

2 个答案:

答案 0 :(得分:0)

在任意位置设置OnSharedPreferenceChangeListener,然后在回调中检查您感兴趣的密钥,并在更改首选项时更新片段

答案 1 :(得分:0)

您应该使用OnSharedPreferenceChangeListener :)在回调中,您可以执行所需的任何布局更改。

您可能会发现此问题很有用:

How to detect if changes were made in the preferences?