Passing an unserializable object to a Handler (from child thread)
To give you a bit of history - I'm basically trying to send an unserializable object (Spanned) from the child thread to the main thread (via Message). I tried the ob开发者_如何转开发vious - turning it into an array of bytes and sending it that way, but that gives an error since it's not implementing serializable.
Is there any other way I can send it using a Bundle? Or something else?
Here is how I'm sending the message in the child thread
// message and bundle for the questions explanation
Message qemsg = messageHandler.obtainMessage();
Bundle qeb = new Bundle();
qeb.putString("questionExplanations", questionExplanations);
qemsg.setData(qeb);
qemsg.arg1 = 0;
messageHandler.sendMessage(qemsg);
Here is the main thread handler (receives the messages sent from the child thread):
private Handler messageHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
CFAData cd = CFAData.getSingletonObject();
Bundle summaryBundle = msg.getData();
switch(msg.arg1) {
case 0:
// receives the bundle here and does what it needs on the UI thread
//testQuestionsExplanations.append(spannedExplanationsObj);
break;
default:
break;
}
}
};
Here's how you attach arbitrary objects to a Message:
qemsg.obj = mySpanned;
精彩评论