public class UIHandler<T> extends Handler {
protected WeakReference<T> ref;
public UIHandler(T cls){
ref = new WeakReference<T>(cls);
}
public T getRef(){
return ref != null ? ref.get() : null;
}
使用方法
private static class UIMyHandler extends UIHandler<HandlerActivity>{
public UIMyHandler(HandlerActivity cls) {
super(cls);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
HandlerActivity activity = ref.get();
if (activity != null){
if (activity.isFinishing())
return;
switch (msg.what){
case 1:{
break;
}
// ...
}
}
}
}
private UIMyHandler myHandler = new UIMyHandler(this);