我知道,如果您Handler
直接初始化 a 并在 a 中使用它,Activity
它会泄漏Context
(由Alex Lockwood提供),例如
public class SampleActivity extends Activity {
private final Handler mLeakyHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
/* ... */
}
}
}
所以我使用了以下方法
private static class StaticHandler extends Handler {
private final WeakReference<SampleActivity> mSampleAct;
private StaticHandler(SampleActivity act) {
mSampleAct = new WeakReference<SampleActivity>(
act);
}
}
然后我将它初始化如下
public class SampleActivity extends Activity {
private Handler myHandler = new StaticHandler(this) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
processTask(msg.obj);
}
};
}
我想知道我的Handler
ie myHandler 是否仍然会泄漏Context
,我怎么能确定它不会?