package cn.nec.nlc.example.jamesli.activitytest53preference;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceFragment;

// suggest to use PreferenceFragment to control the display of your settings instead of
// PreferenceActivity when possible.
public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences1);
        ListPreference listPreference = (ListPreference) findPreference
                (SettingsActivity.KEY_PREF_SYNC_CONN_TYPE);
        listPreference.setSummary("Sync Conn Type: "
                + listPreference.getEntry() + " - " +listPreference.getValue());
    }

    @Override
    public void onResume() {
        super.onResume();
        // register in order to let onSharedPreferenceChanged() valid
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

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

    // Listener method was not initiated, must register the listener in the first place
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals(SettingsActivity.KEY_PREF_SYNC_CONN_TYPE)) {
            ListPreference listPreference = (ListPreference) findPreference(key);
            listPreference.setSummary("Sync Conn Type: "
                    + listPreference.getEntry() + " - " +listPreference.getValue());
        }
    }
}

@@ -393,16 +393,24 @@
 Fragment
+%0A       
  impleme
@@ -1192,32 +1192,1711 @@
 Listener(this);%0A
+          // Note: When you call registerOnSharedPreferenceChangeListener(), the preference manager%0A          // does not currently store a strong reference to the listener. You must store a strong%0A          // reference to the listener, or it will be susceptible to garbage collection. We%0A          // recommend you keep a reference to the listener in the instance data of an object that%0A          // will exist as long as you need the listener.%0A//        // For example, in the following code, the caller does not keep a reference to the listener.%0A//        // As a result, the listener will be subject to garbage collection, and it will fail at some%0A//        // indeterminate time in the future:%0A//        prefs.registerOnSharedPreferenceChangeListener(%0A//                // Bad! The listener is subject to garbage collection!%0A//                new SharedPreferences.OnSharedPreferenceChangeListener() %7B%0A//                    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) %7B%0A//                        // listener implementation%0A//                    %7D%0A//                %7D);%0A//        // Instead, store a reference to the listener in an instance data field of an object that%0A//        // will exist as long as the listener is needed:%0A//        SharedPreferences.OnSharedPreferenceChangeListener listener =%0A//                new SharedPreferences.OnSharedPreferenceChangeListener() %7B%0A//                    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) %7B%0A//                        // listener implementation%0A//                    %7D%0A//                %7D;%0A//        prefs.registerOnSharedPreferenceChangeListener(listener);%0A
     %7D%0A%0A    @Over

Add comment ATest53 for OnSharedPreferenceChangeListener