views:

84

answers:

2

I have a ListActivity containing an object I've defined called a MessageItem. I want to pass the data in this MessageItem off to a Service to do some stuff, but I don't want the Activity to be data bound to the Service such that Android can't reclaim the Activity if it needs to (ergo memory leak).

What I'm doing is passing the MessageItem object to a method in a singleton class along with the application Context. The singleton method uses the application Context to start my Service. I pass to this Service an Intent. In this Intent, I put items from the MessageItem object like so:

    intent.putExtra("subject", msg.getSubject());
    intent.putExtra("summary", msg.getSummary());
    intent.putExtra("created_on", msg.getCreatedDate());
    intent.putExtra("read", msg.getIsRead());

Will this data bind my Activity to the Service the Intent gets passed into and cause a memory leak?

Would this be a better implementation?:

    intent.putExtra("subject", new String(msg.getSubject()));
    intent.putExtra("summary", new String(msg.getSummary()));
    intent.putExtra("created_on", new Integer(msg.getCreatedDate()));
    intent.putExtra("read", new Boolean(msg.getIsRead()));
+2  A: 

Will this data bind my Activity to the Service

No, the code above will the references in the activity to the intent. The string and boolean values have no pointer back to the instance.

Would this be a better implementation?

No! That would be much worse. A lot of data would be copied and a lot of objects would be created for naught.

Aaron Digulla
Thank you for your clarification
Andrew
+2  A: 

You don't need to new String or Integer. When the data is transfered by intent, It will be parceled and unparceled.

imcaptor
Ah yes, good good
Andrew
Is parceled what Android calls it, rather than just calling it boxed?
R. Bemrose