Teo*_*ald 9 android parcel parcelable
我正在实现Parcelable,以便在整个Intent中传输一些简单的数据.
但是,Parcelable界面中有一种方法我完全不懂:newArray().
它没有任何相关文档,甚至在我包裹/删除我的对象时甚至没有在我的代码中调用它.
示例Parcelable实现:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是:这个方法是什么?什么时候打电话?
除了return new MyParcelable[size];那种方法之外,做其他事情还有什么意义吗?
它是在那里准备没有所有泛型的东西的类型数组.而已.
只返回标准return new MyParcelable[size];是好的.
这是正常的,你永远不会自己称呼它.但是,通过调用像Bundle.getParcelableArray()你这样的方法间接地结束这个方法.
| 归档时间: |
|
| 查看次数: |
1356 次 |
| 最近记录: |