-1


我想知道什么时候使用 parcelable 什么时候不使用 . 我知道 parcelable 是 android 中 parcel 复杂数据类型的一种方式,但根据官方文档http://developer.android.com/guide/faq/framework.html#3,没有提及。那么什么时候真正需要parcelable?

4

2 回答 2

0

To pass data to another activity, we are usually put bundle object on activity intent that will be called. Bundle can be filled with primitive data types like long, integer, and boolean. It can be filled with simple data type like String class to represent text. For example, an activity calls another activity at the same time sends simple data to it.

In destination activity, we check the bundle. If it is exist, we open the data of bundle from the origin activity. Now, how if we want to pass the complex data type like our defined class object to another activity? For this need, we can use Parcelable in Android.

Parcelable is an interface for classes so a class that implements Parcelable can be written to and read from a Parcel. The data in Parcel form can be passed between two threads. Parcel itself is a class that have abilities to serialize and deserialize object of class.

于 2014-02-12T04:56:29.507 回答
0

通过实现Parcelable接口,您可以使您的类对象能够存储在 a 中Bundle,然后您可以在Intent.

例如

Intent i = new Intent(....);
i.putParcelableExtra("name", object);

然后在另一个活动中,像这样得到它:

YourClass object = (YourClass) getIntent().getExtras().getParcelableExtra("name");

注意类型转换。为了使用你的类的方法,这是必要的。

于 2013-11-13T05:41:19.687 回答