0

I have followed the Google dev docs and come up with the following shared preference activity:

public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {


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

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();
    }


    public static class SettingsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preferences);
        }

    }


    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Pref changed", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getApplicationContext().getSharedPreferences(this.getLocalClassName(), MODE_PRIVATE).registerOnSharedPreferenceChangeListener(this);
        //Toast.makeText(this, "registered pref listener", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        getApplicationContext().getSharedPreferences(this.getLocalClassName(), MODE_PRIVATE).unregisterOnSharedPreferenceChangeListener(this);
    }

}

As you can see, I have a simple toast message in the onSharedPreferenceChanged method so I can see it working, but it doesn't seem to work.

The only difference with the Google docs is that I am using getApplicationContext() instead of getPreferenceScreen() in the onResume(), because getPreferenceScreen() is showing up as deprecated.

I just need to figure out why its not listening - my preference screen is just full of checkboxes, so I would assume, when I click any of them, the onSharedPreferenceChanged() method is called and I would see my toast.

This simple one has got me stumped.

1 Answer 1

2

You are registering your listener to a wrong preference.

Loaded preference from XML resource in SettingsFragment uses default shared preferences so you should register the listener like this:

@Override
public void onResume() {
  super.onResume();
  SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
  pref.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
  SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
  pref.unregisterOnSharedPreferenceChangeListener(this);
  super.onPause();
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.