3

Lots of similar questions but I still can't find an answer. So I have a Fragment where I want to listen for changes in a PreferenceActivity.

The documentation says to register the listener in onResume() and un-register in onPause():

For proper lifecycle management in the activity, we recommend that you register and unregister your SharedPreferences.OnSharedPreferenceChangeListener during the onResume() and onPause() callbacks, respectively:

I assume they are referring to registering the listener from within the PreferenceActivity onResume/onPause methods, since if you unregister the listener in the Fragment's onPause(), then it will not listen for the changes.

So is the solution to do the opposite i.e. - register in onPause() and un-register in onResume()?

Thanks!

Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
rnorman3
  • 49
  • 1
  • 8
  • Thanks again, in the end I launched the preferenceactivity for result and responded to the preference changes in onActivityResult(). This was mainly because of something unusual I was doing in the PreferenceActivity, which was that when one preference changed, it has effect on 2 other preferences. So I am programmaticaly changing preferences in the preferenceactivity, and this caused the listener to trigger 3 times which I didn't want. – rnorman3 Apr 28 '15 at 15:17

1 Answers1

-1

It's not the PreferenceActivity that notify the changes on the preferences but the SharedPreferences it's self. The PreferenceActivity is a meare user interface to access the SharedPreferences.

So if it's your fragment that needs to listen for SharedPreferences changes it should be the one to register for changes. In other words let your fragment implement the OnSharedPreferenceChangeListener and register for this changes on the fragment onResume and unregister onPause as the doc say.

You also need to load the preference on the fragment onResume so when it goes onPause then onResume the current state of the preference is loaded.

See also https://stackoverflow.com/a/13596569/665823

Community
  • 1
  • 1
Kalem
  • 1,132
  • 12
  • 33
  • OK thanks Kalem. However i still don't understand your answer. Here's why: When i select the menu option "SETTINGS" my fragment is paused, and the settings window appears. Therefore (if i do what you say above - unregister in onPause()) the fragment is no longer listening for changes, so what's the point? – rnorman3 Apr 27 '15 at 21:11
  • For your usecase maybe there's no point. Just the `onresume` behaviour will do. Anyways if you register onpause your fragment won't receive the change event either cause it's inactive (correct me if am wrong). – Kalem Apr 28 '15 at 08:07
  • Thanks again, in the end I launched the preference for result and responded to the preference changes in onActivityResult(). This was mainly because of something unusual I was doing in the PreferenceActivity, which was that when one preference changed, it has effect on 2 other preferences. So I am programmaticaly changing preferences in the preferenceactivity, and this caused the listener to trigger 3 times which I didn't want. – rnorman3 Apr 28 '15 at 15:16