Design question basically - having a PreferenceActivity should one make it implement OnSharedPreferenceChangeListener or should one define this fun
Doing registration and de-registration in onPause/onResume is lots of extra work for nothing.
You could make an anonymous implementation of your listener as part of your class like this:
[class level]
...
OnSharedPreferenceChangeListener mListener = new OnSharedPreferenceChangeListener () {
onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// your code here
}
};
...
[class level]
Then you set it wherever applicable. If you do this, your listener won't be re-created as a different object between onPause/onResume (unless your app gets killed and your Activity subclass has to be loaded again), so assigning it then is pointless, since you'll always reference the same object. On the other hand, if your app does get killed, then the onCreate will be called again.
With regards to implementing an inner class, or not, I tend to prefer having an anonymous implementation (as shown above) due to the enhanced cleanliness of the code - I don't have to bother with class names and have to type fewer brackets. However, it's really a preference thing, so do whatever you feel is better.
I don't believe there are any major reasons to favour a particular location for the listener, apart from personal preference. Having the Activity implement it, or using an inner class - either anonymous or not - are all OK.
The only thing is, if you're not using an existing object like your Activity as the listener, you'll need to keep a reference to the listener object. As per this answer it will get garbage collected (and thus not actually listen to anything) if you don't.
Having dug into the source a bit, it seems SharedPreferencesImpl uses a WeakHashMap to contain registered listeners (source, lines 72-73, 186-196), meaning that failing to unregister won't cause a leak.
As you say, the docs do recommend using onResume() / onPause(); this is probably nothing to do with leaks, but instead to prevent background apps doing unnecessary processing - so still worth following!