tags:

views:

123

answers:

1

I have read some question here but I didn't find a solution. I have read about Parcelable, Intents, and sharing specific data within Activities from the android dev docs (both dev guide and reference).

Here's the scenario:

I have one ListActivity that fills in an object parsing an xml file, it shows a list of values, and when clicked I want to return the object that represents the item clicked to the activity that has called it, for then, call another activity with this object.

I read on how to implement Parcelable but seems not being the way. Implementing Parcelable receives a Parcel for the constructor and then reads the values from it (or at least that was what I understood). This makes no sense for me and I can't see how to implement basing on that issue. I build the object parsing the xml file, not having a Parcel.

I appreciate some clarifications on this, regards.

A: 

I believe you have three options here:

  1. Pass some arbitrary 'id' of the object to the new activity in the intent extras, which obtains the object in the same way as the first activity. This I would recommend as it is in harmony with the way Android is designed to work.
  2. Serialize the object using Java Serializable, then put it into the intent as an extra.
  3. Have the object be a JSONObject and send it as a string in the intent extras.
Jim Blackler
I'm interested in 1. For that, the new activity should "see" the collection in order to obtain the respective object. How can I have this kind of visibility within activities?
Sebastian
I do this by creating a custom Application class that maintains the Collection (or whatever object you want to share between Activities). You can then get the Application from your activity with getApplication(). I'm not sure this is the best approach though so I'd be interested in what other options there are.
Adrian
Yes, I'm doing that too ugly. As I can parse the xml file (it's not an xml bundled with android resources, it's totally defined by me) from anywhere I want, I just use an ID of the list of objects the list has to offer. Then, the ListActivity returns on clicked the position of the list. But, I need to parse the file again, fill another object, and pick the ID-th one in the list. I REALLY don't like that.
Sebastian
Finally, I have done the same as you Adrian. At least I don't parse twice the file and is data required across all the application. Implementing `Parcelable` would be a headache for the complex structure objects I have.
Sebastian