首页 > 代码库 > BaseHandler的封装, 处理handler中的内存泄漏

BaseHandler的封装, 处理handler中的内存泄漏

package de.bvb.study.common;

/**
 * 用于规范 Message.what此属性,避免出现魔法数字
 */
public final class What {

    public static final int ZERO = 0;
    public static final int ONE = 1;

    /** 标记异步操作返回时目标界面已经消失时的处理状态 */
    public static final int ACTIVITY_GONE = -1;
}
package de.bvb.study.common;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;

import java.lang.ref.WeakReference;

import de.bvb.study.util.LogUtil;

/**
 * handler包装类
 */
public abstract class BaseHandler extends Handler {

    protected WeakReference<Activity> activityWeakReference;
    protected WeakReference<Fragment> fragmentWeakReference;

    private BaseHandler() {}//构造私有化,让调用者必须传递一个Activity 或者 Fragment的实例

    public BaseHandler(Activity activity) {
        this.activityWeakReference = new WeakReference<>(activity);
    }

    public BaseHandler(Fragment fragment) {
        this.fragmentWeakReference = new WeakReference<>(fragment);
    }

    @Override
    public void handleMessage(Message msg) {
        if (activityWeakReference == null || activityWeakReference.get() == null || activityWeakReference.get().isFinishing()) {
            // 确认Activity是否不可用
            LogUtil.i("Activity is gone");
            handleMessage(msg, What.ACTIVITY_GONE);
        } else if (fragmentWeakReference == null || fragmentWeakReference.get() == null || fragmentWeakReference.get().isRemoving()) {
            //确认判断Fragment不可用
            LogUtil.i("Fragment is gone");
            handleMessage(msg, What.ACTIVITY_GONE);
        } else {
            handleMessage(msg, msg.what);
        }
    }

    /**
     * 抽象方法用户实现,用来处理具体的业务逻辑
     *
     * @param msg
     * @param what {@link What}
     */
    public abstract void handleMessage(Message msg, int what);

}
package de.bvb.study.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;

import de.bvb.study.common.BaseHandler;
import de.bvb.study.common.What;

public class LoginActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        new Thread(new Runnable() {
            @Override
            public void run() {
                handler.obtainMessage(What.ZERO, "data 0").sendToTarget();
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                handler.obtainMessage(What.ONE, "data 1").sendToTarget();
            }
        }).start();

    }

    MyHandler handler = new MyHandler(this);
    private static class MyHandler extends BaseHandler {

        public MyHandler(Activity activity) {
            super(activity);
        }

        @Override
        public void handleMessage(Message msg, int what) {
            switch (what) {
                case What.ZERO:
                    break;
                case What.ONE:
                    break;
                case What.ACTIVITY_GONE:
                    break;
            }
        }
    }
}

 

BaseHandler的封装, 处理handler中的内存泄漏