Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to perform Serialization gracefully in Activity data Transmission

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to serialize data elegantly in Activity transmission. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

We often need to transfer data between Activity. There are three ways to transfer data.

Use the putExtra () method of Intent to carry a small amount of data to transfer data

Use the putExtra () method of Intent to carry Bundle to transmit data. Do not use this method to transmit important information because it is in clear text.

Use the putExtra () method of Intent to transfer data with serialized objects.

And there are three ways to serialize objects.

Use the Serializable interface provided by Java

Convert to a JSON string using the Gson library

Use the Parcelable interface provided by Android

The use and performance comparison of these three methods are described below.

0x01 Serializable interface

Model layer

The entity class to be passed through Intent implements the Serializable interface

Send data

Accept data

Call the getSerializableExtra () method to get the serialized object

0x02 JSON string

Model layer

It is no different from defining entity classes in normal times.

Send data

You need to use Gson or Fastjson third-party libraries to convert objects into json data.

I'll use Gson as a demonstration here.

Accept data

It also uses JSON libraries to convert json data into objects.

0x03 Parcelable

Model layer

The entity class to be passed through Intent implements the Parcelable interface

And implement two methods:

DescribeContents: content API description. 0 is returned by default.

WriteToParcel: package the passed data into a Parcel container

At the same time, you need to instantiate Parcelable.Creator

Interface for reading data from a Parcel container

Send data

Accept data

Call the getParcelableExtra method to get the serialized object

Performance comparison of 0x04

From the picture, we know that the efficiency of the conversion speed:

Parcelable > Serializable > JSON string

Parcelabled conversion efficiency obviously dropped the Serializable and JSON strings several blocks.

To sum up:

To serialize objects, it is best to use the Parcelable interface

The above is how to serialize the Activity transmission data that Xiaobian shared with you. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 240

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report