Analysis on the method of passing objects using intent in Android programming

This paper analyzes the method of using intent to transfer objects in Android programming. Share with you for your reference, as follows:

In the previous article, I introduced the use of intent, such as starting activities, sending broadcasts, heuristic services, etc., and can transfer some data when using intent. The following code is shown:

When transferring data, the method used is putextra. The supported data types are limited. How to transfer objects??

In Android, there are two ways to pass objects using intent: serializable serialization and Parcelable serialization.

1. Serializable mode

This means that an object is converted into a storable or transportable state. The serialized object can be transmitted on the network and stored locally.

Object serialization, you only need to implement the serializable class.

How does intent pass object parameters? Check the API and find the following methods:

Therefore, this method is used to transfer, as follows:

So how to get it? Use the following methods:

This gives you the EMP object.

2. Parcelable mode

The implementation principle of this method is to decompose a complete object so that each part of the decomposition is the data type supported by intent. Examples are as follows:

The object is passed in the same way as serialization:

Get object:

Serializable generates a large number of temporary variables during serialization, resulting in frequent GC. Therefore, when using memory, Parcelable has higher performance than serializable, so it is recommended to use the Parcelable class.

Parcelable cannot be used when the data is to be stored on disk, because Parcelable cannot guarantee the continuity of the data in case of external changes. Although the efficiency of serializable is low and it is not recommended to use it, it is recommended to use serializable in this case.

I hope this article will help you in Android programming.

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
分享
二维码
< <上一篇
下一篇>>