1

我有以下问题: onSharedPreferenceChanged仅在我第一次更改中的值时调用MultiSelectListPreference,但再也不会调用。当我重新启动 PrefernceActiviy 时,所有首选项都保留了,并且onSharedPreferenceChanged只在第一次调用时再次调用。

我做过的事情:

  1. 我将我PreferenceActivity视为“OnSharedPreferenceChangeListener”(我不创建匿名类)
  2. 我在和中注册和注销this对象onResume()onPause
  3. 我尝试以SharedPreference两种方式获得,作为
    a) getPreferenceScreen().getSharedPreferences()
    a) new SharedPreferences(this)
    但两种方式的工作方式完全相同

我究竟做错了什么?


发布代码:

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 _ =>
        }
    }
}
4

1 回答 1

-1

我遇到了类似的问题,我找到了解决方案。我不知道它是否可以帮助你,但无论如何我都会解释它,希望它会有用。

我最初将监听器放在 PreferenceActivity 中,但它没有按我的意愿工作,所以我将 PreferenceChangeListener 移到了我的 MainActivity(因为在我的应用程序中,首选项可以在 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。

于 2013-07-12T23:17:34.947 回答