通过在意向性对象引用没有实现Serializable或Parcelable [英] Pass Object reference within Intent without implementing Serializable or Parcelable

查看:199
本文介绍了通过在意向性对象引用没有实现Serializable或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/Broadcasts). Everything is done within my process. That's why I see no reason to write my objects into Streams just to reassemble them a few milliseconds after. I want to pass my object reference through my application. Is there any way to do this.

由于我的应用程序将播出一排相同的事件多次,我不能依靠静态成员。我需要exacly相同的对象为我播放。

Since my application will broadcast the same Event multiple times in a row I can't rely on static members. I need to get exacly the same object for what I broadcasted.

这就是为什么我在想一个静态的Referenceholder,将接受一个对象,并返回一个整数,标识此对象在它的内部列表,以便我可以通过.putExtras通过这个整数。但据我了解Java,我不能从这个名单收拾这个对象就被添加之后,因为多个侦听器可以在同样的对象中interessted,我必须保持它在我的Referenceholder永远(假设一个线程可在任何时间恢复 - 甚至2分钟后)

That's why I was thinking about a static "Referenceholder" that will accept an Object and return an integer that identifies this object in it's internal list so I can pass this integer via .putExtras. But as far as I know Java I could not clean up this Object from this list after it has been added because multiple Listeners could be interessted in the very same object and I would have to keep it in my Referenceholder for ever (assuming that a thread may be resumed at any time - even 2 minutes later).

任何想法?难道我做错了什么?或如何,我可以清理我的referneces任何想法(可能在几秒钟后?这可能会导致崩溃,但它似乎比写code的组装和重组我的对象没有任何理由更适用)

Any ideas? Am I doing something wrong? Or any ideas of how I can clean up my referneces (probably after some seconds? this may lead to a crash but it seems to be more applicable than writing code that assembles and reassembles my objects for no reason)

推荐答案

您的选择是pretty的明确:有没有办法通过一个未marshallable对象(Parcelable,序列化)的一个意图。句号。

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

你也许能够做的就是传递东西是一个引用一个未marshallable对象。我们的想法是,你会做一些路过一键映射的关键,你有兴趣的合格值的地图的顺序。如果意图发送方和接收方的意图都可以访问的地图,你可以传达一个参考联合国marshallable对象。

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.

我不明白,就是这样,为什么你认为静态成员是不是你想要的。我猜想,静态地图,在自定义的应用对象,将是pretty的多正是你想要的。 ......我怀疑,从约WeakHashMaps的评论,你已经发现了这一点。

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.

最好的运气

这篇关于通过在意向性对象引用没有实现Serializable或Parcelable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆