java 在 Intent 中传递对象引用而不实现 Serializable 或 Parcelable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14917029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:50:41  来源:igfitidea点击:

Pass Object reference within Intent without implementing Serializable or Parcelable

javaandroidandroid-intentandroid-activityreference

提问by Chris

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.

我需要通过意图(活动调用/广播)传递复杂的对象。一切都在我的过程中完成。这就是为什么我认为没有理由将我的对象写入 Streams 只是为了在几毫秒后重新组装它们。我想通过我的应用程序传递我的对象引用。有没有办法做到这一点。

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.

由于我的应用程序将连续多次广播同一事件,因此我不能依赖静态成员。我需要为我广播的内容获得完全相同的对象。

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).

这就是为什么我正在考虑一个静态的“Referenceholder”,它将接受一个对象并返回一个整数,在它的内部列表中标识这个对象,这样我就可以通过 .putExtras 传递这个整数。但是据我所知 Java,在添加此对象后,我无法从该列表中清除此对象,因为多个侦听器可能对同一个对象感兴趣,并且我必须将它永远保留在我的 Referenceholder 中(假设一个线程可以随时恢复 - 甚至 2 分钟后)。

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)

有任何想法吗?难道我做错了什么?或者关于如何清理我的引用的任何想法(可能在几秒钟后?这可能会导致崩溃,但它似乎比编写无缘无故组装和重新组装我的对象的代码更适用)

回答by G. Blake Meike

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

您的选择非常明确:无法在 Intent 中传递不可编组的对象(Parcelable、Serializable)。句号。

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.

您可能能够做的是传递一些对不可编组对象的引用。这个想法是,您将按照将键传递给映射的顺序执行某些操作,该映射将该键映射到您有兴趣传递的值。如果 Intent 发送者和 Intent 接收者都可以访问映射,则可以传递对不可编组对象的引用。

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.

我不明白为什么你认为静态成员不是你想要的。我猜想在自定义 Application 对象中的静态地图几乎正是您想要的。...我怀疑,从你对 WeakHashMaps 的评论中,你已经发现了这一点。

... 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

祝你好运

回答by Chris

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...

忘记这个解决方案。这是行不通的。Android 正在处理您在 .startActivity() 中传递的 Intent。无法在活动中获得任何引用。这是 - 在我看来 - 很棒但是我的谷歌。你必须调用你的活动并将你的对象的引用放在静态成员中......

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

正如 G. Blake Meike 所提到的,在 Android 中无法通过 Intent 传递对象引用。但是你也许可以使用WeakReferences

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

关于这个主题的非常优秀的文章可以在这里找到: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?

我通过这个问题找到了解决方案: 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 WeakHashMapand 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).

所以我基本上要做的是:我将使用 Intent 作为WeakHashMap的键,并将我的对象作为值传递。这似乎是唯一适合作为 Key 的 Object,因为您放入 Intents extras 的所有内容都将被序列化。因此,每个 Intent 只能传递一个对象。你可以在你的 Acitivity 中实现子类,它可以保存你的对象,而不是把这个子类放到地图中。但是我仍然不确定接收者将获得的 Intent 对象是否与调用者创建的对象相同,但我认为是这样。如果不是,我将编辑此解决方案(或者也许有人可以解决这个问题)。