可打包,newArray用于什么? [英] Parcelable, what is newArray for?
问题描述
我正在实现Parcelable,以便在整个Intent中传输一些简单的数据.
但是,Parcelable接口中有一种我根本不了解的方法: newArray()
.
它没有任何相关文档和内容.包裹/取消包裹对象时,甚至没有在我的代码中调用它.
I am implementing Parcelable in order to transmit some simple data throughout an Intent.
However, There is one method in the Parcelable interface that I don't understand at all : newArray()
.
It does not have any relevant documentation & is not even called in my code when I parcel/deparcel my object.
可打包实施示例:
public class MyParcelable implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
所以,我的问题是:这种方法是做什么用的?什么时候叫?
除了在该方法中 return new MyParcelable [size];
之外,还有什么别的意思吗?
So, my question is : what is this method for ? and when is it called ?
Is there any point in doing something else than return new MyParcelable[size];
in that method ?
推荐答案
当您尝试反序列化 Parcelable
对象的数组并对每个单个对象 createFromParcel进行序列化时,将调用此函数
被调用.
this is a function to be called when you try to deserialize an array of Parcelable
objects and for each single object createFromParcel
is called.
这篇关于可打包,newArray用于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!