永远不会执行OnSharedPreferenceChangeListener回调

时间:2015-03-04 20:05:02

标签: android sharedpreferences android-preferences

我的主要活动是开始新活动(SearchResultsActivity)。 我还有一个设置菜单。

我希望每次设置菜单中有新的/已编辑的设置时都会通知我SearchResultsActivity

我正在OnSharedPreferenceChangeListener上实现SearchResultsActivity接口并注册自己以收听设置中的任何修改,但由于某些奇怪的原因,回调永远不会被执行。

这是我的SearchResultsActivity代码

public class SearchResultsActivity extends BaseActivity implements SearchResultsFragment.RouteResultsFragmentCommunicator, SharedPreferences.OnSharedPreferenceChangeListener {

    private static final String TAG = SearchResultsActivity.class.getSimpleName();

    private FragmentManager mFragmentManager;
    private SearchResultsFragment mSearchResultsFragment;
    private RouteFragment mRouteFragment;
    private SharedPreferences pref;


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

        setContentView(R.layout.activity_search_results);

        // Set the toolbar
        activateToolbarWithHomeEnabled();

        pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        // Initialize the FragmentManager
        mFragmentManager = getFragmentManager();

        mSearchResultsFragment = (SearchResultsFragment) mFragmentManager.findFragmentById(R.id.searchRouteResultsFragment);
    }


    @Override
    protected void onResume() {
        super.onResume();

        pref.registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        EventBus.getDefault().unregister(this);

        super.onStop();
    }

    @Override
    protected void onPause() {
        pref.unregisterOnSharedPreferenceChangeListener(this);
        super.onPause();
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        Log.d(TAG, "onSharedPreferenceChanged callback");
    }

}

为了简单起见,我删除了与我的问题无关的部分代码。

所以我的问题是,当我对任何设置进行更改时,“onSharedPreferenceChanged回调”永远不会打印在logcat上。

以防万一,这是我的设置实现:

public class MySettings extends BaseActivity {

    private static final String TAG = MySettings.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pref_with_actionbar);

        // Set the toolbar
        activateToolbarWithHomeEnabled();

        getFragmentManager().beginTransaction().replace(R.id.content_frame, new MyPreferenceFragment()).commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id==android.R.id.home) {
            finish();
            return true;
        }
        else {
            return super.onOptionsItemSelected(item);
        }
    }
}

片段:

public class MyPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

    private static final String TAG = MyPreferenceFragment.class.getSimpleName();

    private EditTextPreference mMaxRadius;
    private SharedPreferences mSharedPreferences;
    private CheckBoxPreference mCheckBoxPreference;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        mMaxRadius = (EditTextPreference) findPreference(getResources().getString(R.string.pref_radius_key));

        mSharedPreferences = getPreferenceManager().getSharedPreferences();
        String maxRadius = mSharedPreferences.getString(getResources().getString(R.string.pref_radius_key), "Unknown");
        mMaxRadius.setSummary("Max Radius: " + maxRadius);

        mCheckBoxPreference = (CheckBoxPreference) findPreference(getString(R.string.pref_order_list_key));
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equalsIgnoreCase(getResources().getString(R.string.pref_radius_key))) {
            Preference connectionPref = findPreference(key);
            // Set summary to be the user-description for the selected value
            connectionPref.setSummary(sharedPreferences.getString(key, "Unknown") + " km");
        }
        else if (key.equalsIgnoreCase(getString(R.string.pref_order_list_key))) {
            Preference orderByPreference = findPreference(key);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }
}

请注意,MyPreferenceFragment onSharedPreferenceChanged按预期调用MyPreferenceFragment#onSharedPreferenceChanged,eveytime会更改设置SearchResultsActivity#onSharedPreferenceChanged。但SearchResultsActivity#onSharedPreferenceChanged从未被调用过。

有人可以解释一下,当我在设置上更改某些内容时,{{1}}从未执行过什么问题吗?

1 个答案:

答案 0 :(得分:2)

onCreate()注册您的听众,并在onDestroy()取消注册。当您启动首选项活动时,您的主要活动将暂停,并取消注册。