Android Intent传递parcelable对象与传递Json字符串

时间:2022-07-05 18:04:47

I am aware that there are some answers to my question, but the answers are not very elaborate or cogent. In my program, in order to transmit data from one activity to other I am converting the contents of the POJO class into a normal string and passing (METHOD1).

我知道我的问题有一些答案,但答案不是很精细或有说服力。在我的程序中,为了将数据从一个活动传输到另一个活动,我将POJO类的内容转换为普通字符串并传递(METHOD1)。

METHOD1

方法1

String jsonString = JacksonSingleton.getObjectMapper().writeValueAsString(object);
bundle.putExtra(JSON_STRING_KEY, jsonString)

METHOD2

方法2

bundle.putExtra(JSON_STRING_KEY, parcelableObject);

My question is which of these two methods are preferable only in terms of performance? I have always believed that making a class parcelable or serializable is inefficient. In METHOD 1, I don't need to make any alterations to my POJO class.

我的问题是这两种方法中的哪一种仅在性能方面更可取?我一直认为制作一个可分类或可序列化的类是低效的。在方法1中,我不需要对我的POJO类进行任何更改。

1 个解决方案

#1


15  

As much as I hate answering my own questions, I thought I'd discuss and share my observations which would help others in future.

尽管我讨厌回答我自己的问题,但我想我会讨论并分享我的观察,这将有助于其他人。

Transmission of data from one activity to another can be done using either passing Serializable or Parcelable Objects in an activity intent. Android developer website recommends usage of Parcelable interface for this purpose (link).

可以使用在活动意图中传递Serializable或Parcelable对象来完成从一个活动到另一个活动的数据传输。 Android开发人员网站建议为此目的使用Parcelable接口(链接)。

However, my question was pertaining to the efficiency comparison between passing Parcelable object and JSON string.

但是,我的问题与传递Parcelable对象和JSON字符串之间的效率比较有关。

To test this, I used an old and low end Android device. I launched an activity by sending a large Parcelable Object in an activity intent. Next I launched the same activity using the JSON String of the same object in the activity intent. What I observed was a significant observable latency while launching an activity by sending a JSON String instead of Parcelable Object.

为了测试这个,我使用了旧的和低端的Android设备。我通过在活动意图中发送一个大的Parcelable对象来启动一个活动。接下来,我使用活动意图中相同对象的JSON字符串启动了相同的活动。我观察到的是通过发送JSON字符串而不是Parcelable Object来启动活动时的显着可观察延迟。

In conclusion, Even if we pass a JSON String, a String object implements Serializable. Google recommends using Parcelable instead of Serializable objects. This will usually be insignificant in case of strings of negligible length. However in case of massive Json Strings of massive objects, The efficiency will certainly take a toll.

总之,即使我们传递JSON字符串,String对象也会实现Serializable。 Google建议使用Parcelable而不是Serializable对象。如果字符串长度可以忽略不计,这通常是微不足道的。然而,如果有大量Json Strings的大型物体,效率肯定会造成损失。

You could refer this for performance benchmark of Parcelable vs Serializable.

你可以参考它来获得Parcelable和Serializable的性能基准。

#1


15  

As much as I hate answering my own questions, I thought I'd discuss and share my observations which would help others in future.

尽管我讨厌回答我自己的问题,但我想我会讨论并分享我的观察,这将有助于其他人。

Transmission of data from one activity to another can be done using either passing Serializable or Parcelable Objects in an activity intent. Android developer website recommends usage of Parcelable interface for this purpose (link).

可以使用在活动意图中传递Serializable或Parcelable对象来完成从一个活动到另一个活动的数据传输。 Android开发人员网站建议为此目的使用Parcelable接口(链接)。

However, my question was pertaining to the efficiency comparison between passing Parcelable object and JSON string.

但是,我的问题与传递Parcelable对象和JSON字符串之间的效率比较有关。

To test this, I used an old and low end Android device. I launched an activity by sending a large Parcelable Object in an activity intent. Next I launched the same activity using the JSON String of the same object in the activity intent. What I observed was a significant observable latency while launching an activity by sending a JSON String instead of Parcelable Object.

为了测试这个,我使用了旧的和低端的Android设备。我通过在活动意图中发送一个大的Parcelable对象来启动一个活动。接下来,我使用活动意图中相同对象的JSON字符串启动了相同的活动。我观察到的是通过发送JSON字符串而不是Parcelable Object来启动活动时的显着可观察延迟。

In conclusion, Even if we pass a JSON String, a String object implements Serializable. Google recommends using Parcelable instead of Serializable objects. This will usually be insignificant in case of strings of negligible length. However in case of massive Json Strings of massive objects, The efficiency will certainly take a toll.

总之,即使我们传递JSON字符串,String对象也会实现Serializable。 Google建议使用Parcelable而不是Serializable对象。如果字符串长度可以忽略不计,这通常是微不足道的。然而,如果有大量Json Strings的大型物体,效率肯定会造成损失。

You could refer this for performance benchmark of Parcelable vs Serializable.

你可以参考它来获得Parcelable和Serializable的性能基准。