Skip to content

Instantly share code, notes, and snippets.

@garymabin
Created September 17, 2014 02:58
Show Gist options
  • Save garymabin/0268abaca2ad18ecf21c to your computer and use it in GitHub Desktop.
Save garymabin/0268abaca2ad18ecf21c to your computer and use it in GitHub Desktop.
Code for exchanging Parcelable data between work thread and main thread
public interface INotifier {
void onNewData(Parcelable payload);
}
public class WorkThread {
private INotifier mNotifier;
public void run() {
Parcelable payload;
//do your network relate stuff
mNotifier.onNewData(payload);
}
}
public class WorkTracker implements INotifier {
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
//run on ui thread now!
Parcelable payload = (Parcelable)msg.obj;
}
};
public void onNewData(Parcelable payload) {
//called on WorkThread, use Handler to change to main thred.
mHandler.sendMessage(Message.obtain(null,0,0,payload));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment