public class MainActivity extends Activity {
private final Handler mLeakyHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
...
}
}
}
但是这样使用的时候,lint工具会提示警告
In Android, Handler classes should be static or leaks might occur, Messages enqueued
on the application thread’s MessageQueue also retain their target Handler.
If the Handler is an inner class, its outer class will be retained as well.
To avoid leaking the outer class, declare the Handler as a static nested class
with a WeakReference to its outer class