Raj
Raj

Reputation: 67

Passing objects in android

I want to pass a client object to a diffrent activity on android

I know how to pass strings but have no idea about passing objects.

myIntent.putExtra("nick",nick); where nick is a string

how do i pass an object say Client c?

Upvotes: 0

Views: 311

Answers (4)

JAL
JAL

Reputation: 3319

Caveat: the standard line is use simple primitives/Strings as name:value pairs, prefer parcelable over serializable

The argument is that passing serialized objects is inefficient, so do not do it. For a small object I am not so sure that this is a real world concern. It has been argued for readability and clarity over efficiency unless you detect poor performance. So far with a small object I have not detected any performance problems. IMHO, using the fully qualified name of the class in the name:value pair and serializable objects makes the code readable, simple , less buggy, easier to maintain and is easy to implement during prototyping. If performance problems are detected, or you have time to refactor the code base, then the serializable code can be converted to Parcelable code once the object properties have stabilized.

OK. I am putting on my flame suit.

Upvotes: 0

Geoff
Geoff

Reputation: 773

Think hard about whether you need to send the object or if you really just need a few data elements out of the object. Sending the primitives or Strings will likely be much simpler (= faster, potentially less buggy) if it's enough to meet your needs.

If you do need to pass an object then you could either implement the Parcelable interface or you could put the object into a static variable (of type List, Set, Map, etc.) in another class and reference it that way. There are drawbacks to these approaches and I would only recommend them if you can't get by passing the actual data values you need through the bundle.

Upvotes: 0

Erik Nedwidek
Erik Nedwidek

Reputation: 6184

If the activity you are passing the object to is your own, serialize the object into a string and deserialize in the activity. http://java.sun.com/developer/technicalArticles/Programming/serialization/

Or as answered here: How to pass an object from one activity to another on Android

Upvotes: 0

user634545
user634545

Reputation: 9419

"If you're just passing objects around then Parcelable was designed for this. It requires a little more effort to use than using Java's native serialization, but it's way faster (and I mean way, WAY faster)."

How to send an object from one Android Activity to another using Intents?

Upvotes: 1

Related Questions