安卓:应该在哪里OnShared preferenceChangeListener定义/注册定义、OnShared、preferenceChangeListener

2023-09-05 00:27:53 作者:颈上草莓印

设计问题基本上 - 有一个 preferenceActivity 应该让它执行 OnShared preferenceChangeListener 或人应该在另一个类中定义这个功能 - 说,在一个内部类?是否有某种原因,人们会preFER了一个比​​其他的方法呢?

Design question basically - having a PreferenceActivity should one make it implement OnSharedPreferenceChangeListener or should one define this functionality in another class - say in an inner class ? Is there some reason one would prefer the one over the other approach ?

此外应该在哪里一个寄存器监听器?我的意思是的文档和常识决定到注册/注销的 onResume /的onPause 分别,但在看到一个 < A HREF =htt​​p://stackoverflow.com/a/13669689/281545>数不胜数 注册在的onCreate 我只是想知道如果我失去了一些东西。

Also where should one register the listener ? I mean the docs and common sense dictate to register/unregister in onResume/onPause respectively but having seen a zillion registrations in onCreate I just wonder if I am missing something.

另外我不是很肯定,如果没有注销(这里例如注销所以也不能称其为的onStop 不能保证被调用)将必然导致泄漏。所以,如果我有,例如

Also I am not quite certain if a failure to unregister (so here for instance unregistering may not be called as onStop is not guaranteed to be called) would necessarily lead to a leak. So if I have for instance

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

这会泄露我的preF 比如有一次我要回到我的其他活动?

Would this leak the MyPref instance once I go back to one of my other activities ?

最后 - 将相同的考虑也适用于在preferenceChangeListener

Lastly - would the same considerations apply to OnPreferenceChangeListener ?

编辑:回来给我看没有办法真正注销在preferenceChangeListener - 我是瞎了??

Edit : coming back to that I see no way to actually unregister the OnPreferenceChangeListener - am I blind ??

推荐答案

我不相信有任何主要原因有利于特定位置的听众,除了个人preference。具有活动执行它,或者使用一个内部类 - 匿名或不 - 是一切OK

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.

已经挖成一点,似乎共享preferencesImpl 源使用的WeakHashMap 包含注册听众(来源,线72-73,186-196),这意味着未注销的不会的引起泄漏。

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.

正如你所说,该文档不推荐使用 onResume() / 的onPause();这可能是无关的泄漏,而是以prevent后台应用程序做不必要的处理 - !所以还是值得以下

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!