Chinaunix首页 | 论坛 | 博客
  • 博客访问: 494286
  • 博文数量: 135
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 905
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-24 19:31
文章分类

全部博文(135)

文章存档

2010年(135)

我的朋友

分类: Java

2010-04-13 20:32:34

Question:
Quote:
What is the difference between a Bundle and a Parcel and a Message?
What are the interactions between each. A detailed explanation would
go a long way to explaining the whole of the android.os package for
me.


Answer:
Quote:
Hello!

A Bundle is functionally equivalent to a standard Map. The reason we didn't
just use a Map is because in the contexts where Bundle is used, the only
things that are legal to put into it are primitives like Strings, ints, and
so on. Because the standard Map API lets you insert arbitrary Objects, this
would allow developers to put data into the Map that the system can't
actually support, which would lead to weird, non-intuitive application
errors. Bundle was created to replace Map with a typesafe container that
makes it explicitly clear that it only supports primitives.

A Parcel is similar to a Bundle, but is more sophisticated and can support
more complex serialization of classes. Applications can implement the
Parcelable interface to define application-specific classes that can be
passed around, particularly when using Services. Parcelables can be more
sophisticated than Bundles, but this comes at a cost of significantly higher
overhead.

Bundle and Parcel are both data serialization mechanisms, and for the most
part both are used when application code is passing data across processes.
However, because Parcel is much higher overhead that Bundle, Bundles are
used in the more common places like the onCreate method, where overhead must
be as low as possible. Parcels are most commonly used to allow applications
to define Services with logical APIs that can use application-meaningful
classes as method arguments and return values. If we required Bundle there,
it would result in really clunky APIs. You should in general still keep
your Service APIs as simple as possible, because primitives will serialize
more efficiently than custom Parcelable classes.

The Message class is very different. A Message is really just a way for an
application to send messages between threads, and is not related to data
serialization. Since accesses to Views must be single-threaded, the
MessageHandler and related Message classes are commonly used to make it
easier to pass messages between threads.

Hope that helps!

- Dan

阅读(1155) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~