我们如何决定是否使用writeTypeList / readTypedList或writeList / readList

时间:2017-07-23 16:52:25

标签: android

目前,我仍然想知道何时为某个类实施Parcelable界面,我们如何决定何时使用writeTypedList/ readTypedListwriteList/ readList

// Using writeTypedList
parcel.writeTypedList(watchlistColumnTypes);

// Using writeList
parcel.writeList(watchlistColumnTypes);

// Using readTypedList
watchlistColumnTypes = new ArrayList<>();
in.readTypedList(watchlistColumnTypes, WatchlistArrayRecyclerViewAdapter.ColumnType.CREATOR);


// Using readList
watchlistColumnTypes = new ArrayList<>();
in.readList(watchlistColumnTypes, WatchlistArrayRecyclerViewAdapter.class.getClassLoader());

对我来说都是可行的。但是,我不确定两者之间有什么区别,我们如何在它们中间做出选择呢?

1 个答案:

答案 0 :(得分:1)

我认为经验法则是,我们应尽可能使用writeTypedListreadTypedList,并在需要时使用writeListreadList

这是因为writeTypedListreadTypedList似乎有更好的表现。

根据https://android.googlesource.com/platform/frameworks/base/+/1a008c1/core/java/android/os/Parcel.java#117

  

还有一些方法可以提供更有效的工作方式   与Parcelables:writeTypedObject(T,int),writeTypedArray(T [],int),   writeTypedList(List),readTypedObject(Parcelable.Creator),   createTypedArray(Parcelable.Creator)和   createTypedArrayList(Parcelable.Creator)。这些方法不写   原始对象的类信息:相反,是调用者的   read函数必须知道期望什么类型并传入   适当的Parcelable.Creator代替正确构建新的   反对并阅读其数据。