Perhaps what is newArray for?

I am implementing Parcelable to pass some simple data in all 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 and is not even called in my code when I send / separate my object.

Example. Possible implementation:

 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(); } } 

So my question is: what is this method for? and when is it called?
Does it make sense to do something else besides return new MyParcelable[size]; in this method?

+8
android parcelable parcel
source share
2 answers

this is a function that is called when trying to deserialize an array of Parcelable objects and for each individual createFromParcel object.

+6
source share

Here you need to prepare a typed array without any generics. What is it.
Return only standard return new MyParcelable[size]; OK.

It’s normal that you never call it yourself. However, by calling something like Bundle.getParcelableArray() , you find yourself in this method indirectly.

+4
source share

All Articles