Created
March 22, 2015 11:01
-
-
Save shirish87/c96e7bb661a072cad7e5 to your computer and use it in GitHub Desktop.
Handler to get around Context memory leak issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.apparcadia.common.lib; | |
import android.os.Handler; | |
import android.os.Message; | |
import java.lang.ref.WeakReference; | |
public abstract class MessageHandler<T> extends Handler { | |
private final WeakReference<T> ref; | |
public MessageHandler(T t) { | |
super(); | |
ref = new WeakReference<>(t); | |
} | |
@Override | |
public final void handleMessage(Message msg) { | |
super.handleMessage(msg); | |
T t = ref.get(); | |
if (t == null) { | |
return; | |
} | |
handleMessage(t, msg); | |
} | |
public abstract void handleMessage(T t, Message msg); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment