在一个应用程序是什么传递活动之间的自定义对象的最佳方式是什么? [英] Within an application what is the best way to pass custom objects between activities?

查看:97
本文介绍了在一个应用程序是什么传递活动之间的自定义对象的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问多一些时间,我经历过很多这样的问题......几乎所有这些问题抛了使用parcelable接口为你的类。 然而,在一对夫妇的问题,我遇到了一个报价:

I know this question has been asked multiple number of times and i have been through a lot of these questions......almost all of these questions throw up the use of the parcelable interface for your class. However in a couple of questions i came across a quotation:

注意:看Parcelable可能引发的问题,为什么机器人不能使用 内置Java序列化机制?事实证明,Android团队得出的结论 在的Java序列化实在太慢,以满足Android的进程间通信 要求。因此,团队构建的Parcelable解决方案。的Parcelable方法需要 您明确序列化类的成员,但最终,你会得到更快更 序列化的对象。 同时意识到,Android提供了两种机制,可让您将数据传递给另一个 处理。第一是使用意图束传递到活动,第二是通过一个 Parcelable到服务。 这两种机制是不能互换的,不应该 困惑。也就是说,Parcelable并不意味着要传递到活动中。如果你想开始 一个活动,并通过它的一些数据,使用一个包。 Parcelable旨在仅用作的一部分 一个AIDL定义。

"NOTE: Seeing Parcelable might have triggered the question, why is Android not using the built-in Java serialization mechanism? It turns out that the Android team came to the conclusion that the serialization in Java is far too slow to satisfy Android’s interprocess-communication requirements. So the team built the Parcelable solution. The Parcelable approach requires that you explicitly serialize the members of your class, but in the end, you get a much faster serialization of your objects. Also realize that Android provides two mechanisms that allow you to pass data to another process. The first is to pass a bundle to an activity using an intent, and the second is to pass a Parcelable to a service. These two mechanisms are not interchangeable and should not be confused. That is, the Parcelable is not meant to be passed to an activity. If you want to start an activity and pass it some data, use a bundle. Parcelable is meant to be used only as part of an AIDL definition."

这句话也可以在书中找到专业的Andr​​oid 2。

This quote can also be found in the book Pro Android 2.

现在看到了,在同一个进程中相同的应用程序运行(<一个范围内的所有活动href="http://stackoverflow.com/questions/6468126/every-activity-in-android-is-a-process-or-one-application-is-one-process">Every在Android的活动是一个过程,或一个应用是一个进程),除非在清单中另有规定,同一应用程序的活动范围内通信是不是每个se.So进程间通信是不是真的更快地使用parcelable类或只是足够的通过你的对象,通过属性的意图通过捆绑?

Now seeing that all activities within the same application run in the same process(Every Activity in Android is a Process,or One Application is one process),unless otherwise specified in the manifest,communication within the activities of the same application is not Interprocess communication per se.So is it really faster to use the parcelable class or is it just enough to pass your object attributes through bundle via intent ?

脱落任何光线在这方面将主要由AP preciated。 干杯!!

Shedding any light on this aspect will be largely appreciated. Cheers !!

推荐答案

有一个常见问题解答为了那个原因。 :)

There's a FAQ for that. :)

简短的回答是,Android团队推荐三种方法中的一个应用程序传递活动和服务之间的数据:一个单例类;公共静态字段或方法;在WeakReferences的一个HashMap到对象(和你传递的意图的关键)。主要的问题,要记住的是如何将数据会表现在不同的生命周期事件。 (例如,如果用户打开手机,在默认情况下你的活动会被破坏并重新创建。您的数据处理方法需要在设计时考虑到这一点)

The short answer is that the Android team recommends three techniques for passing data between activities and services within an app: a singleton class; a public static field or method; a HashMap of WeakReferences to Objects (and you pass the key in the intent). The major issue to keep in mind is how your data is going to behave under various lifecycle events. (For instance, if the user turns the phone, by default your activities will be destroyed and recreated; your data handling method needs to be designed with that in mind.)

这篇关于在一个应用程序是什么传递活动之间的自定义对象的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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