我有以下问题:
onSharedPreferenceChanged
仅在我第一次更改MultiSelectListPreference
中的值时才会调用,但永远不会再次调用onSharedPreferenceChanged
。当我重新启动PrefernceActiviy时,所有首选项都保留,并且仅在第一次调用PreferenceActivity
时再次调用。
我做过的事情:
this
视为' OnSharedPreferenceChangeListener' (我不会创建匿名课程)onResume()
和onPause
SharedPreference
个对象
object PreferencesActivity {
final val KEY_AUTO_REFRESH = "preference_auto_refresh"
final val KEY_REFRESH_PERIOD = "preference_refresh_period"
final val KEY_NOTIFICATIONS = "preference_notifications"
final val KEY_AUTO_MARK_AS_READ = "preference_auto_mark_as_read"
final val KEY_CHECKED_RADIO_BUTTON = "preference_checked_radio"
final val KEY_SHOW_SELECTED_ONLY = "preference_show_selected_only"
final val KEY_KEEP_READ_FEEDS = "preference_keep_read_feeds"
private final val DEFAULT_REFRESH_PERIOD = "10"
}
class PreferencesActivity extends SherlockPreferenceActivity with OnSharedPreferenceChangeListener {
private lazy val sharedPreferences = getPreferenceScreen.getSharedPreferences
override def onCreate(savedInstanceState: Bundle) = {
super.onCreate(savedInstanceState)
addPreferencesFromResource(R.xml.preferences)
getSupportActionBar.setDisplayHomeAsUpEnabled(true)
}
override def onResume = {
super.onResume
getPreferenceScreen.getSharedPreferences
.registerOnSharedPreferenceChangeListener(this)
configureRefreshPeriod
configureNotificationsType
}
override def onPause = {
super.onPause
getPreferenceScreen.getSharedPreferences
.unregisterOnSharedPreferenceChangeListener(this)
}
private def configureRefreshPeriod = {
val textViewRefreshPeriod = findPreference(KEY_REFRESH_PERIOD)
textViewRefreshPeriod.setSummary(getNotificationPeriod)
}
private def getNotificationPeriod = {
val refreshPeriodValue = sharedPreferences.getString(KEY_REFRESH_PERIOD, DEFAULT_REFRESH_PERIOD)
val summaryRefreshPeriod_general = getString(R.string.preference_period_summary)
val summaryRefreshPeriod_concrete = String.format(summaryRefreshPeriod_general, refreshPeriodValue.toString)
summaryRefreshPeriod_concrete
}
private def configureNotificationsType = {
val textViewNotifications = findPreference(KEY_NOTIFICATIONS)
textViewNotifications.setSummary(getNotificationKinds)
}
private def getNotificationKinds = {
val notificationsSet = sharedPreferences.getStringSet(KEY_NOTIFICATIONS, new HashSet)
if (!notificationsSet.isEmpty)
getString(R.string.preference_notifications_summary_checked) +
getStringValuesFromSet(notificationsSet)
else
getString(R.string.preference_notifications_summary_unchecked)
}
private def getStringValuesFromSet(notificationsSet: Set[String]) = {
notificationsSet.toArray.mkString(", ")
}
override def onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) =
key match {
case `KEY_REFRESH_PERIOD` => configureRefreshPeriod
case `KEY_NOTIFICATIONS` => configureNotificationsType
case _ =>
}
}
}
,因为
我做错了什么?
发布代码:
{{1}}
答案 0 :(得分:-1)
我遇到了类似的问题,我找到了解决方案。我不知道它是否可以帮助你,但无论如何我都会解释它,希望它会有用。
我最初将Listener放在PreferenceActivity中,但它没有按照我的意愿工作,所以我将PreferenceChangeListener移动到了我的MainActivity(因为在我的App偏好中可以在不同的位置从PreferenceActivity进行更改)。
我在MainActivity.java中创建了这个类:
private class PreferenceChangeListener implements OnSharedPreferenceChangeListener {
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// Function to manage changes
somethingChanged();
}
}
我在MainActivity onCreate方法中实例化它:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// mPreferenceListener is a private attribute of MainActivity.java
mPreferenceListener = new PreferenceChangeListener();
settings.registerOnSharedPreferenceChangeListener(mPreferenceListener);
[..]
}
通过这种方式,监听器正确拦截任何首选项更改,包括MultiSelectListPreference。