4

Hey I'm trying to add an onSharedPreference Listener into n Fragment. It should regulate if a change is done (switch in the settings) the value of the NumberPicker inside the Fragment changes(0 or 1) too.

the problem with the listener: it only works the first 5-10 times then it doesnt get called anymore (i suppose)? -> no changes done on NumberPicker following the importants code of my fragment:

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;
}

The docs say that i need to change the onResume() and onPause() to:

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

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

But where do i need to add these? Got errors when i tried to add it in the Fragment.

Thanks for helping; didnt come to any solution myself.. :/

Sebastian
  • 408
  • 1
  • 6
  • 11
  • 1
    Have you tried changing the `onResume()` and `onPause()` methods to `public void` ? – OrhanC1 Nov 03 '15 at 21:10
  • yeah, but then the error appears : cannot resolve method 'getPreferenceScreen()' – Sebastian Nov 03 '15 at 21:13
  • Could you give me a link to the guide you're using please? – OrhanC1 Nov 03 '15 at 21:14
  • http://developer.android.com/guide/topics/ui/settings.html#Listening http://stackoverflow.com/questions/2542938/sharedpreferences-onsharedpreferencechangelistener-not-being-called-consistently those links , but in general is it right to add the listener inside the fragment? some add it inside the preferencefragment but i dont know how i could thenget access to my numberpicker from the fragment – Sebastian Nov 03 '15 at 21:22
  • Please try having `SharedPreferences.OnSharedPreferenceChangeListener listener ` as a `field` and move it outside the `onCreateView` – OrhanC1 Nov 03 '15 at 21:25
  • what do you mean with as a field? hm moved it simply into the constructor but now prefs.registerOnSharedPreferenceChangeListener(listener); cannot be resolved :/ – Sebastian Nov 03 '15 at 21:31
  • I mean move the listener underneath your `enteredWeight` variable. I've also added an answer with example code :) – OrhanC1 Nov 03 '15 at 21:34

1 Answers1

4

The documentation says

Caution: When you call registerOnSharedPreferenceChangeListener(), the preference manager does not currently store a strong reference to the listener

I recommend that you store your instance of SharedPreferences.OnSharedPreferenceChangeListener listener as a field outside of onCreateView.

Example code:

public class FragmentTwo extends 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;
}
OrhanC1
  • 1,415
  • 2
  • 14
  • 28
  • 1
    okey thanks first :D changed these, but do i still need the onPause() and onResume() methods? because they still got the error with the getPreferenceScreen() – Sebastian Nov 03 '15 at 21:38
  • Ahh yes. Please try making the `pref` a field variable (like `listener`), and call `prefs.getSharedPreferences() .unregisterOnSharedPreferenceChangeListener(this);` – OrhanC1 Nov 03 '15 at 21:43