Android的 - 爪哇 - 通过意图,束或Parcelables并传递价值? [英] android - java - pass values through intents, bundles or Parcelables?
问题描述
上下文:
Android版
我读了很多的净例子传递活动之间DATAS。
我所学到的:
- 通过意图传递原始DATAS(intent.putExtra(键,值))
- 捆绑原始DATAS并把它放在一个intent.putExtra(钥匙,包对象)
- Parcelables并 - >对复杂对象和原语
在你说,是的,我的确实的错过了一些东西。
问:
那么,它似乎有多种方式来达到同样的目标。
如果我想通过一个自定义的对象,Parcelable是我的男人。但我还是会要序列化,所以到最后,我也会有元,对吧?
要通过原语,我为什么要使用一个包时,直接以传递意图将使它呢?
Android的文档/论坛/博客的例子没有做对我来说。我的C背景仍持有我回了一下。
有3个不同的方式为什么要实现一个目标,顺便说一下?
但我还是要序列化,所以到最后,我也会有元,对吧?
块引用>在Java的pretty所有事情最终还是归结到元。
要通过原语,我为什么要使用一个包时,直接以传递意图将使它呢?
块引用>无论哪种方式工作。使用哪种让你感觉更舒服。然而,就注意上的按键冲突(例如,你的活动,而且你继承你的一些基本活动,都试图把同样的事情在
意图$的相同的密钥C $ C>或
捆绑
)。
为什么有3种不同的方式来实现一个目标,顺便说一下?
块引用>我要猜一个目标就是从一个活动传递到另一个数据。涉及的进程间通信(IPC),即使这两种活动都在相同的过程中,作为芯OS过程参与路由。对于流之外pretty多凡事都从
的ContentProvider
在标准的Android,IPC意味着数据必须被放入包裹
,这将会转换成字节数组传递跨进程边界。
Parcelable
重新presents可添加到自定义类的接口,让他们被放入一个包裹
。
捆绑
是一个具体的类,它实现Parcelable
和再presents一个HashMap的
状结构,但强类型到被称为是能够进入一个包裹
的东西。捆绑
比更方便的地块
为开发者,因为它提供了通过键,其中<$ C $随机访问C>包裹没有。
意图
演员只是一个捆绑
,为此,意图
暴露自己的访问方法。
有关我的所有的情况下,我应该忘记其他可能性,并且只使用Parcelable?
块引用>据我所知,有什么EpicPandaForce指的是
序列化
和Parcelable
之间的比较。这两者都可以进入一个捆绑
或包裹
。在这两个,其他条件不变,序列化
比较慢,因为它假定序列化形式必须是持久的,能够被数月或数年后再次读入。Parcelable
假定每个人都在努力从相同的类定义的,可以绕过某些序列化
开销的结果。话虽这么说,这是一个微型的优化。
为什么其他仍然存在呢?
块引用>不是什么都可以做,延长
Parcelable
,特别是的java。*
类。整数
,例如,是不是Parcelable
。然而,我们希望能够通过周围INT
和整数
值。因此,包裹
支持INT
一样,捆绑
。
你的意思是我应该拿回来的东西R.blah
块引用>据我所知,在评论,Marconcini先生指的是ID作为一个笼统的概念,不是指
R.id
特别值。例如,如果你保持影像缓存,而不是绕过实际位图
对象,通过周围的一些标识指向回缓存。Context:
Android
I read a lot of examples on the net for passing datas between activities.
What I learned :
- Pass primitive datas through Intents (intent.putExtra(key,value))
- Bundle the primitive datas and put it in an intent.putExtra(key,bundle object)
- Parcelables -> for complex objects and primitives.
Before you say it, yes, I certainly missed something.
Question:
Well, it seems that there are multiple ways to achieve the same goal.
If I want to pass a custom object, a Parcelable is my man. BUT I will still have to "serialize" it, so in the end, I will also have primitives, right ?
To pass primitives, why should I use a bundle when passing directly in an intent will make it too ?
The android documentation / forums / blogs examples did not make it for me. My C background still holds me back a bit.
Why having 3 different ways to achieve one goal, by the way ?
解决方案BUT I will still have to "serialize" it, so in the end, I will also have primitives, right ?
Pretty much everything in Java eventually boils down to primitives.
To pass primitives, why should I use a bundle when passing directly in an intent will make it too ?
Either way works. Use whichever makes you feel more comfortable. However, just watch out for collisions on keys (e.g., your activity, and a some base activity of yours that you inherit from, both trying to put the same thing in the same key of the
Intent
orBundle
).Why having 3 different ways to achieve one goal, by the way ?
I am going to guess that "one goal" is "to pass data from one activity to another". That involves inter-process communication (IPC), even if the two activities are in the same process, as a core OS process is involved in the routing. For pretty much everything outside of streams from a
ContentProvider
in standard Android, IPC means that data has to be put into aParcel
, which gets converted into a byte array for passing across the process boundary.
Parcelable
represents an interface that can be added to custom classes to allow them to be put into aParcel
.
Bundle
is a concrete class that implementsParcelable
and represents aHashMap
-like structure, but strongly typed to things that are known to be able to go into aParcel
.Bundle
is more convenient than aParcel
for developers, in that it offers random access by key, whereParcel
does not.
Intent
extras are merely aBundle
, for whichIntent
exposes its own accessor methods.For all my cases, I should forget about the other possibilities and only use Parcelable ?
AFAIK, what EpicPandaForce was referring to was the comparison between
Serializable
andParcelable
. Either can go into aBundle
or aParcel
. Of the two, all else being equal,Serializable
is slower, because it assumes that the serialized form has to be durable, able to be read in again months or years later.Parcelable
assumes that everyone is working off of the same class definition and can bypass someSerializable
overhead as a result.That being said, it's a micro-optimization.
Why are the other still there then ?
Not everything can be made to extend
Parcelable
, notablyjava.*
classes.Integer
, for example, is notParcelable
. Yet, we would like to be able to passint
andInteger
values around. Hence,Parcel
supportsint
, as doesBundle
.you mean that I should get it back with the R.blah thing
AFAIK, in that comment, Mr. Marconcini was referring to "id" as a general concept, not referring to
R.id
values specifically. For example, if you are maintaining an image cache, rather than passing around actualBitmap
objects, pass around some identifier that points back to the cache.这篇关于Android的 - 爪哇 - 通过意图,束或Parcelables并传递价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!