Skip to content Skip to sidebar Skip to footer

Pass Object Reference Within Intent Without Implementing Serializable Or Parcelable

I know similar questions have been asked multiple times. I think i read most of it. But none answer is applicable. I need to pass complex Objects via Intents (Activity calls/Broadc

Solution 1:

Your options are pretty clear: There is no way to pass an un-marshallable object (Parcelable, Serializable) in an Intent. Full stop.

What you might be able to do is to pass something that is a reference to an un-marshallable object. The idea is that you would do something on the order of passing a key to a map that maps that key to the value that you are interested in passing. If both the Intent sender and the intent recipient have access to the map, you can communicate a reference to the un-marshallable object.

I don't understand, exactly, why you think static members are not what you want. I would guess that a static map, in a custom Application object, would be pretty much exactly what you want. ... and I suspect, from your comment about WeakHashMaps, that you've discovered exactly that.

... except that nothing you've said so far explains why you want to make your map Weak. Before you use a Weak map, have a look at Soft references, to make sure that that is not what you mean.

Best of luck


Solution 2:

EDIT:

Forget about this solution. It does not work. Android is coping the Intent that you pass in .startActivity(). There is no way to get any reference inside a activity. This is - in my opinion - great bu****t my by google. You have to call your activity and place the referneces of your object in static members...

As metioned by G. Blake Meike, there is no way to pass Object references in Android via Intents. But you maybe can use WeakReferences.

A very excelent aticle about this topic is found here: http://weblogs.java.net/blog/2006/05/04/understanding-weak-references

I got to that solution through this question: Is it possible to get the object reference count?

So what I'm basically going to do is: I will use Intents as a Key for a WeakHashMap and pass my Object as value. This seems to be the only Object that is suitable as Key since everything you put into the Intents extras will be serialized. Due to that, you can only pass one Object per Intent. You could implement Subclasses inside your Acitivity that can hold your Objects an put this Subclass into the map instead. But I'm still not sure if the Intent object that a receiver will get is the same that the caller created but I think so. If it is not, I will edit this solution (or maybe someone could clear that up).


Post a Comment for "Pass Object Reference Within Intent Without Implementing Serializable Or Parcelable"