目前,我仍然想知道何时为某个类实施Parcelable
界面,我们如何决定何时使用writeTypedList/ readTypedList
或writeList/ 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());
对我来说都是可行的。但是,我不确定两者之间有什么区别,我们如何在它们中间做出选择呢?
答案 0 :(得分:1)
我认为经验法则是,我们应尽可能使用writeTypedList
或readTypedList
,并在需要时使用writeList
或readList
。
这是因为writeTypedList
或readTypedList
似乎有更好的表现。
还有一些方法可以提供更有效的工作方式 与Parcelables:writeTypedObject(T,int),writeTypedArray(T [],int), writeTypedList(List),readTypedObject(Parcelable.Creator), createTypedArray(Parcelable.Creator)和 createTypedArrayList(Parcelable.Creator)。这些方法不写 原始对象的类信息:相反,是调用者的 read函数必须知道期望什么类型并传入 适当的Parcelable.Creator代替正确构建新的 反对并阅读其数据。