当ListPreference更改值时,不会调用SharedPreferences.OnSharedPreferenceChangeListener

时间:2017-03-26 17:26:41

标签: android android-preferences

我打开了一个简单的项目,以简化测试我无法解决的问题 - 当用户更改ListPreference的值时,为什么不调用我的监听器。通过重新运行应用程序可以看到值已更改。

preferences.xml很简单:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <ListPreference
        android:title="list preference"
        android:key="@string/pref_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:persistent="true"
        android:entryValues="@array/all_blah"
        android:entries="@array/all_blah"
        android:summary="%s" />

</PreferenceScreen>

它指的是strings.xml

中的以下条目
<string-array name="all_blah">
    <item>blah 1</item>
    <item>blah 2</item>
    <item>blah 3</item>
    <item>blah 4</item>
</string-array>
<string name="pref_list">pref_list</string>
<string name="pref_file">SharedPrefFile</string>

MainActivity非常简单:

package com.acme.settingstest;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent modifySettings=new Intent(MainActivity.this, SettingsActivity.class);
                startActivity(modifySettings);
            }
        });
    }
}

最后,SettingsActivity是:

package com.acme.settingstest;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

class SettingsActivity extends AppCompatActivity {

    SharedPreferences mPrefs = null;
    private static final String TAG = "SettingsActivity";

    private final SharedPreferences.OnSharedPreferenceChangeListener mListener =
            new SharedPreferences.OnSharedPreferenceChangeListener() {
                public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
                    if (key.equals(getString(R.string.pref_list))) {
                        Log.e(TAG, "caught change in pref_list");
                    }
                }

            };

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

        mPrefs = getSharedPreferences(getString(R.string.pref_file), Context.MODE_PRIVATE);
        getFragmentManager()
                .beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();
    }

    @Override
    public void onPause() {
        super.onPause();
        mPrefs.unregisterOnSharedPreferenceChangeListener(mListener);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPrefs.registerOnSharedPreferenceChangeListener(mListener);
    }

    public static class SettingsFragment extends PreferenceFragment {

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

1 个答案:

答案 0 :(得分:2)

您在SettingsActivity中使用的是非默认偏好设置,但您并未表明共享偏好应使用的PreferenceManager

OnSharedPreferenceChangeListener已注册到SharedPreferences - SharedPrefFile - 这不是默认值。

在添加首选项之前,在OnCreate的SettingsFragment中尝试设置SharedPreferencesName。像这样:

 PreferenceManager prefMgr = getPreferenceManager();
 prefMgr.setSharedPreferencesName(getString(R.string.pref_file));
 prefMgr.setSharedPreferencesMode(MODE_WORLD_READABLE);