taracus
taracus

Reputation: 393

Is there any other way to pass complex object with Intents?

So I've got quite complex datatypes that needs to be sent through an Intent. All these types comes as part of a base-class (BaseResponse), that have alot of child-classes, these child-classes also includes loads of different complex-datatypes. I would love if there was a way so I only had to implement the Parcelable on for example the child-classes, but it seems all members of classes implementing Parcelable needs to be Parcelable as well?

If the answer is something other than "No, stop being lazy" you win some of my Internets for today.

PS. These types are all auto-generated from a WSDL so I sort of wish I didn't have to edit them all manually. DS.

Quick edit: I know of the Serializable interface but it would pose the same problem.

2nd edit: I noticed you could pass Arrays of Objects through the Intent, Im not sure if this is going to serialize it as an Object (aka with no members) or if I can just type-cast it back when I receive the Array. Ill try it out and report back

3rd edit: The whole putting Object-arrays in the Intent wont work I guess and being lazy I'm going with the JSON-serializing option and marking it as answer (even though I havent tried it but guess it should work if a stranger on the Internet says so)

Final edit: I am now using the Json-serializing way marked as correct answer and it works just fine. There are some limitations to what objects Gson can serialize but nothing that concern me. There is probably a very in-efficient way (in terms of cpu) of doing this but the time it saved me from having to implement the interface explicitly for ~300 classes is worth it. Thank you all for the rapid answers and suggestions, StackOverFlow is without a doubt the best community on all of history of mankind.

Upvotes: 0

Views: 86

Answers (3)

Marcin Orlowski
Marcin Orlowski

Reputation: 75635

Implementing Parcelable for all classes (not sure why it is a surprise to you :) is the right way of doing this on Android.

Upvotes: 1

vipul mittal
vipul mittal

Reputation: 17401

I used GSON library to first convert my object into string(JSON string) and then put into intent extra.

And at the time of receiving it convert it back to original object.

Works fine for me but don't know if it is an efficient way of doing it.

Upvotes: 1

rachit
rachit

Reputation: 1996

There's no other alternative I think, Else you can use Database to fill that data (if possible), and then pass its key (Table's primary key) or something to achieve it. Don't kill me if you have considered it also already! :O

Upvotes: 1

Related Questions