Android - Problem with the Serializable interface - android

I have been using the Serializable interface to pass an object from one activity to another. I am using putExtra on the sender side and getSerializable on the receiver side. Everything works fine but I have received (for the first time) the following error report:
java.lang.RuntimeException: Parcelable encountered IOException reading
a Serializable object
I don't understand why this exception has been generated since I am using getSerializable and not getParcelable.
I know that I should implement the Parcelable interface instead because it has been designed specifically for Android (and that's what I will end up doing) but I want to understand why I am getting this error.
Thanks!

Parcelable is mentioned in this error because an Intent you send from one Activity to another has a Bundle inside and this Bundle is Parcelable. When you call Intent.putExtra() this extra is added to the inner Bundle. When Intent is passed between activities its Bundle is converted to and from a byte array and so is your Serializable object.
But I don't know why this error occurs. Maybe it's because of some bug in writeObject()/readObject() implementation.

Related

How to send object using getserializable extra method

I am storing data in serializable object & sending it to other activity.
But, I have difficulty in finding data in other activity.
Inside 2nd activity. Error here:
Intent in=getIntent.getserializableExtra("ob");
Cast the data to your Serializable object.
`OBJECTCLASS obj=(OBJECTCLASS)getIntent.getserializableExtra("ob");`

Syntax for creating a Object implementing Parcelable from getParcelableExtra(intent_name)

I am implementing the DataDroid model of RESTful communication and I have come across a compiler error while implementing my SearchCriteria. The problem is that in order to pass the SearchCriteria around as an intent extra, I had to make it implement Parcelable. However, my Worker's start function requires a SearchCriteria class, leading to the error: Required: my.classes.SearchCriteria; Found: android.os.Parcelable.
Assuming that I've correctly implemented Parcelable for my SearchCriteria class, how can I quickly create an object from a parcel (where the parcel is found using getParcelable Extra(INTENT_NAME)?
Edit: I realize I can accomplish this quickly by making my constructor for SearchCriteria from Parcel public, but is there another way? Actually, this does not work - I confused a Parcel with a Parcelable thing.
Suppose you follow the API and make SearchCriteria implements Parcelable properly, and your SearchCriteria has been properly constructed or instantiated from the underlying business layer, either from a Database or a Http server or etc.
To pass it to next activity by intent:
SearchCriteria searchCriteria = createSearchCriteria();
Intent intent = new Intent(getBaseContext(), NextActivity.class);
intent.putExtra("searchCriteria", searchCriteria);
startActivity(intent);
To retrieve it from intent in next activity:
SearchCriteria searchCriteria = getIntent().getParcelableExtra("searchCriteria");
myWorker.search(searchCriteria);
In most situation, we don't need bother Parcel directly.

Transport own class with Intent between objects

HI,
i'm programming an android application using the StartActivityForResult() - Method.
But, i want to get as result an own class, not a string or something like that.
how can i tell the intent to use the class i wrote to give back?
You have to have your class implement Parcelable or Serializable. Also, this technique is only recommended for activities within the same application.

Android bundles passing Pointer or copies of objects

If I attach an object to a bundle and store the bundle in an intent and start another activity, does that activity work on a copy of the original object or does it use the same object but just passes along a pointer?
In Java there is no such thing as pointers. You can either pass and object by value or by reference.
CommonsWare listed the possibilities the following way:
Use remote services and AIDL to implement a remote procedure call,
effectively giving you "pass by
reference" between apps
Use Parceable and Intent extras, effectively giving you "pass by value"
between apps
So you pass your objects by value.

Android: Send arbitrary objects within Activities?

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.
I believe you have three options here:
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.
Serialize the object using Java Serializable, then put it into the intent as an extra.
Have the object be a JSONObject and send it as a string in the intent extras.
That depends on whether the activities belong to the same process or different ones...if it is the same process then the answer is Within an application what is the best way to pass custom objects between activities? but if they belong to different processes then you are better of implementing parcelable.

Categories

Resources