Handler Should be static or leaks Occur?

解决办法:

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

posted @ 2016-06-23 11:37  庭庭小妖  阅读(220)  评论(0编辑  收藏  举报