共享偏好的更改未触发

时间:2017-04-25 19:47:22

标签: android sharedpreferences listener

我的布局中有一个浮动操作按钮。用户可以选择左侧或右侧的按钮位置。这可以在首选项中选择。

执行此操作的代码工作正常,但侦听器未检测到首选项的更改。

如果应用程序重新启动,则根据新的首选项显示浮动操作按钮,因此我知道具有此首选项的进程有效,但不幸的是,侦听器似乎失败。

当首选项发生变化时,我需要做些什么才能让侦听器触发?

我的MainActivity中的监听器:

private final SharedPreferences.OnSharedPreferenceChangeListener 
        mPositionFabListener = new SharedPreferences.OnSharedPreferenceChangeListener() {

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        Log.e(TAG,"Are we getting here?");
        // We do not get here when preference has changed. Why not??

        // Code to update Floating Action Button to new position

    }
};

我在MainActivity的onResume()方法中注册了侦听器,并在onPause()中取消注册。

@Override
protected void onResume() {
    super.onResume();
    PrefUtils.registerOnPrefChangeListener(mPositionFabListener);
}

@Override
protected void onPause() {
    PrefUtils.unregisterOnPrefChangeListener(mPositionFabListener);
    super.onPause();
}

registerOnPrefChangeListener方法在单独的类中声明:

public class PrefUtils {

public static boolean getBoolean(String key, boolean defValue) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApplication.getContext());
    return settings.getBoolean(key, defValue);
}

public static void putBoolean(String key, boolean value) {
    SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(MainApplication.getContext()).edit();
    editor.putBoolean(key, value);
    editor.apply();
}

public static int getInt(String key, int defValue) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApplication.getContext());
    return settings.getInt(key, defValue);
}

public static void putInt(String key, int value) {
    SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(MainApplication.getContext()).edit();
    editor.putInt(key, value);
    editor.apply();
}

public static void registerOnPrefChangeListener(OnSharedPreferenceChangeListener listener) {
    try {
        PreferenceManager.getDefaultSharedPreferences(MainApplication.getContext()).registerOnSharedPreferenceChangeListener(listener);
    } catch (Exception ignored) {}
}

public static void unregisterOnPrefChangeListener(OnSharedPreferenceChangeListener listener) {
    try {
        PreferenceManager.getDefaultSharedPreferences(MainApplication.getContext()).unregisterOnSharedPreferenceChangeListener(listener);
    } catch (Exception ignored) {}
}

}

更新: 在GeneralPrefsFragment类中,我将此侦听器放入测试目的。这个听众正在工作。那为什么它在MainActivity中不起作用?

public class GeneralPrefsFragment extends PreferenceFragment {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   Preference  preference = findPreference(PrefUtils.POSITION_FLOATING_MENU_BUTTON);
   Preference.OnPreferenceChangeListener mListener = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            PrefUtils.putBoolean(PrefUtils.POSITION_FLOATING_MENU_BUTTON, Boolean.TRUE.equals(newValue));

            PreferenceManager.getDefaultSharedPreferences(MainApplication.getContext()).edit().commit(); // to be sure all prefs are written

            Log.e(TAG, "The listener in " + TAG + " is listening");

            // This listener is fired as soon as changes in the preference are made!
            // Why is the same kind of listener not fired in the MainActivity?

            return true;
        }
    };
    preference.setOnPreferenceChangeListener(mListener);


}

}

解: 我得到了修复。我仍然不知道为什么以前的设置不起作用,但我得到了它,感谢this answer中的代码。

在MainAcivity的onCreate()方法中,我放置了以下侦听器,当首选项发生变化时,会触发此侦听器。

SharedPreferences prefs =
        PreferenceManager.getDefaultSharedPreferences(this);

SharedPreferences.OnSharedPreferenceChangeListener mPrefsFabListener =
        new SharedPreferences.OnSharedPreferenceChangeListener() {
            public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
                if (PrefUtils.POSITION_FLOATING_MENU_BUTTON.equals(key)) {
                    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
                    CoordinatorLayout.LayoutParams paramsFab = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
                    if (PrefUtils.getBoolean(PrefUtils.POSITION_FLOATING_MENU_BUTTON, false)) {
                        paramsFab.gravity = Gravity.BOTTOM | Gravity.START;
                    } else {
                        paramsFab.gravity = Gravity.BOTTOM | Gravity.END;
                    }
                }
            }
        };
prefs.registerOnSharedPreferenceChangeListener(mPrefsFabListener); 

1 个答案:

答案 0 :(得分:1)

OnSharedPreferenceChangeListener在MainAcitivity的onPause()方法中取消注册。这是一个问题,因为一旦启动“常规首选项活动”,MainActivity就会暂停,以便更改首选项。因此,监听器未注册,因此在需要时无法正常收听!

我从unregisterOnPrefChangeListener中删除了onPause()并输入以下代码。这非常有效!

@Override
protected void onDestroy() {
    PrefUtils.unregisterOnPrefChangeListener(mPositionFabListener);
    super.onDestroy();
}