android: problem with Serializable object put into intent
Asked Answered
H

4

5

Hi i have problem with a class i want to pass in an intent by putting it into the putExtras() Its serializable and the code looks like this:

public abstract class ObjectA extends ArrayList<ObjectA> implements java.io.Serializable{...}

public class ObjectB extends ObjectA {...}


...
Bundle extras = new Bundle();
extras.putSerializable("blabla", ObjectB);
intent.putExtras(extras);

...

Object y = getIntent().getExtras().get("blabla");

the problem is, that y now is an ArrayList and no longer an ObjectB so i cant cast it.. if i change the code to

public class ObjectB implements java.io.Serializable {...}

it works fine

Haulage answered 10/10, 2009 at 17:7 Comment(5)
Just out of curiosity, what is "an intent" in this context?Bildungsroman
ok i just added the android tag (again), an intent is an android internal thingHaulage
Ah. Thanks! 15 characters! Woot!Bildungsroman
ArrayLists don't implement Serializable, you can use a vector or write your own save and load functions for Serializable.Latham
when i surround the obeject which extends arraylist with another object that just implements serial. than there is no problem, so i think the serialization of the arraylist works..?Haulage
C
14

By implementing both java.util.List and java.io.Serializable in your class you've triggered this android bug.

Carolann answered 16/11, 2009 at 17:22 Comment(4)
I think I stumbled on the same issue: if I stuff a LinkedList in a bundle via putSerializable, when I try to get it by getSerializable I receive an ArrayList. Isn't it?Militarism
Yeah, it does seem to goof up LinkedLists in Serializables in multiple circumstances.Brei
Bug from 2009, still here 3 years later :(Touzle
Still here and its 2014 now :(Chrysalid
B
0

I suspect what's happening is that since you aren't declaring ObjectB as serializable it's "falling back" to the most recent parent object that is. So when you put it in to the Bundle you aren't actually putting in ObjectB, but ObjectB cast back to ArrayList.

I think you're going to have to go with the second ("works fine") code.

Branch answered 12/10, 2009 at 23:37 Comment(2)
i now surrounded the ObjectB with a class (wich implements serializable) that wich contains the ObjectB instance. i need objectB to be a child of objectA so i think this is ne best solution. and its just a little change at the codeHaulage
Why can't you just have ObjectB extend ObjectA and implement Serializable?Branch
T
0

How are you declaring ObjectB before you pass it into the bundle? From what I understand of your question you are getting no error when you pass in the bundle, only when removing it. Try retreiving your ObjectB into an ObjectB type directly like this:

ObjectB y = (ObjectB) getIntent().getExtras().get("blabla");
Telamon answered 20/10, 2009 at 14:0 Comment(0)
K
0

The ObjectA in ArrayList should implements the interface Parcelable. After that you can put your arraylists in the intent, and get them in another activity.

Keith answered 12/12, 2009 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.