PreferenceCompatFragment null pointer when setting OnSharedPreferenceChangeListener

63 views Asked by At

I'm using mentioned PreferenceCompatFragment in my code but I have a problem with setting listener on changing settings.

I have made an implementation of OnSharedPreferenceChangeListener in my Activity so I have tried to set mentioned listener in a moment when Fragment will be attached to view/container but all time I'm getting null pointer on PreferenceManager.getDefaultSharedPreferences(getActivity());

Most examples are construct with such way that Fragment is implementing also listener - in my case i want to do it separately.

1

There are 1 answers

0
Robert On

I just found quite simple solution,

  • onCreatePreferences - add preferences from resource
  • onAttach - assign listener (global variable) from MainActivity
  • onCreate - getPreferencesManager and register listener