onSharedPreferenceChange Listener - >在片段中

时间:2015-11-03 20:51:55

标签: android android-fragments sharedpreferences listener android-sharedpreferences

嘿我正在尝试将一个onSharedPreference Listener添加到n Fragment中。它应该调节是否完成更改(切换设置)片段内的NumberPicker的值也会更改(0或1)。

听众的问题:它只能在前5-10次工作,然后它再也不会被调用(我想)? - >没有对NumberPicker进行任何更改 按照片段的重要代码:

public class FragmentTwo extends Fragment {

private EditText mEnterWeight;
private NumberPicker mUnitPicker;
private TextView mConverted;
private int pick;
private String convertedWeightMessage;
private Double enteredWeight;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_two, container, false);
    mEnterWeight = (EditText) view.findViewById(R.id.WCenterWeight);
    mUnitPicker = (NumberPicker) view.findViewById(R.id.WCunitPicker);
    mConverted = (TextView) view.findViewById(R.id.WCconverted);
    initialiseUnitPicker(); 
    //some stuff 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
            // listener implementation      

            if(key.equals("SwitchMainUnit")) {
                Boolean kg = prefs.getBoolean("SwitchMainUnit", true);
                    if(kg)
                        mUnitPicker.setValue(0);
                        else mUnitPicker.setValue(1);
            }

        }
    };
    prefs.registerOnSharedPreferenceChangeListener(listener);
 return view;
}

文档说我需要将onResume()和onPause()更改为:

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

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

但我需要在哪里添加这些?当我试图在片段中添加它时出错。

感谢您的帮助;自己没有找到任何解决方案..:/

1 个答案:

答案 0 :(得分:2)

文档说

  

警告:当你调用registerOnSharedPreferenceChangeListener()时,   偏好管理器目前不存储强引用   听者

我建议您将SharedPreferences.OnSharedPreferenceChangeListener listener的实例存储为onCreateView之外的字段。

示例代码:

公共类FragmentTwo扩展了Fragment {

private EditText mEnterWeight;
private NumberPicker mUnitPicker;
private TextView mConverted;
private int pick;
private String convertedWeightMessage;
private Double enteredWeight;
SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        // listener implementation      

        if (key.equals("SwitchMainUnit")) {
            Boolean kg = prefs.getBoolean("SwitchMainUnit", true);
            if (kg) mUnitPicker.setValue(0);
            else mUnitPicker.setValue(1);
        }

    }
};
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_two, container, false);
    mEnterWeight = (EditText) view.findViewById(R.id.WCenterWeight);
    mUnitPicker = (NumberPicker) view.findViewById(R.id.WCunitPicker);
    mConverted = (TextView) view.findViewById(R.id.WCconverted);
    initialiseUnitPicker();
    //some stuff 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    prefs.registerOnSharedPreferenceChangeListener(listener);
    return view;
}