Android: where OnSharedPreferenceChangeListener should be defined / registered

The question about design is basically - having PreferenceActivity, should it be implemented OnSharedPreferenceChangeListeneror is it necessary to define this functionality in another class - say, in the inner class? Is there any reason someone prefers a different approach?

And where should the listener be registered? I mean documents and common sense dictate to register / unregister onResume/onPauseaccordingly, but seeing a zillion registration in onCreateI am just wondering if I missed something.

In addition, I’m not quite sure that the refusal to register (so here , for example, non- registration cannot be caused because onStopit cannot be called) will certainly lead to a leak. Therefore, if I have, for example,

class MyPref extends PreferenceActivity implements
            OnSharedPreferenceChangeListener {
    SharedPreferences sharedPreferences;
    // init sharedPreferences
    onStart(){
        sharedPreferences.registerOnSharedPreferenceChangeListener(this);
    }
    // no unregistration
}

Will this leak be an instance MyPrefwhen I get back to one of my other actions?

Finally - are the same considerations applicable to OnPreferenceChangeListener?

Edit: back to the fact that I don’t see a way to unregister OnPreferenceChangeListener- am I blind?

+5
source share
2 answers

, - , , . Activity - , - .

, , Activity , . , (, , ), .


, , SharedPreferencesImpl WeakHashMap (source, 72-73, 186-196), , .

, onResume()/onPause(); , , , - !

+1

- onPause/onResume - .

:

[class level]
...
OnSharedPreferenceChangeListener mListener = new OnSharedPreferenceChangeListener () {
    onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        // your code here
    }
};
...
[class level]

, . , onPause/onResume ( , Activity ), , . , , onCreate.

, ( ) - - . , , , , .

0

All Articles