Created
May 8, 2017 06:21
-
-
Save brucetoo/22fa06d6de61deb9e5438d4f434009c7 to your computer and use it in GitHub Desktop.
Handler tools to wrap Activity with WeakReference
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.aliyun.wireless.vos.appstore.shortcut; | |
import android.app.Activity; | |
import android.os.Handler; | |
import android.os.Message; | |
import java.lang.ref.WeakReference; | |
/** | |
* Created by Bruce Too | |
* On 08/05/2017. | |
* At 11:34 | |
*/ | |
public class RefHandler<T extends Activity> extends Handler { | |
private WeakReference<T> activityRef; | |
private MessageCallback<T> callback; | |
public RefHandler(T activity, MessageCallback<T> callback) { | |
activityRef = new WeakReference<T>(activity); | |
this.callback = callback; | |
} | |
@Override | |
public void handleMessage(Message msg) { | |
super.handleMessage(msg); | |
T activity = activityRef.get(); | |
if (activity != null) { | |
callback.handleMessage(activity, msg); | |
} | |
} | |
public interface MessageCallback<T extends Activity> { | |
void handleMessage(T activity, Message msg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment