How to use bundle to transfer objects through intent in Android

In Android development, it is sometimes necessary to transfer objects in applications or between processes. The following describes in detail the method of intent using bundle to transfer objects.

The passed object needs to be serialized first, and there are two ways to serialize the object: Java io. Serializable and Android os. Parcelable

Serializable is used in Java, while Google uses a custom Parcelable in Android.

Differences between the two serialization methods:

1. When using memory, Parcelable has higher performance than serializable. It is recommended to use Parcelable class;

2. Serializable will generate a large number of temporary variables during serialization, resulting in frequent GC;

3. Parcelable cannot be used when the data is to be stored on the disk, because Parcelable cannot guarantee the continuity of the data well. In case of external changes,

Serializable is recommended in this case.

Step 1: define serialization entity class; Serializable mode:

Parcelable mode:

Step 2: pass the serialized object;

Step 3: get the object;

Screenshot of operation results:

Attached source code: source code download

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>