码迷,mamicode.com
首页 > 其他好文 > 详细

Handler Should be static or leaks Occur?

时间:2016-06-23 12:32:06      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

解决办法:

public class SampleActivity extends Activity {
         
  /**
   * Instances of static inner classes do not hold an implicit
   * reference to their outer class.
   */
  private static class MyHandler extends Handler {
    private final WeakReference<SampleActivity> mActivity;
         
    public MyHandler(SampleActivity activity) {
      mActivity = new WeakReference<SampleActivity>(activity);
    }
         
    @Override
    public void handleMessage(Message msg) {
      SampleActivity activity = mActivity.get();
      if (activity != null) {
        // ...
      }
    }
  }
         
  private final MyHandler mHandler = new MyHandler(this);

参考:android handler的警告Handler Class Should be Static or Leaks Occur

Handler Should be static or leaks Occur?

标签:

原文地址:http://www.cnblogs.com/tt2015-sz/p/5610164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!