UPDATE:
IParcelable apparently cannot currently be implemented in Mono for Android. In the end I used the .NET serialization in the class, and then parceled/bundled the serialized data in the Android-specific code, which works just fine. It also keeps the class cross-platform compatible, which is desirable.
ORIGINAL QUESTION:
I’m trying to implement Parcelable as part of a class in a Mono for Android app, but Xamarin’s documentation for Parcelable is copy-pasted from the Android documentation:
http://androidapi.xamarin.com/?link=T%3aAndroid.OS.IParcelable
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();
}
}
Since that documentation is written for Java, it’s basically wrong for C#. I’m just wondering if anyone knows how to convert this code into C#. I’m particularly having trouble with the CREATOR field.
Also, since I’m trying to write code that I can port to other platforms later, what’s the best way to implement Parcelable? Should I make it part of the class using partial classes?
One thing I often forget with Anonymous Inner Classes (AIC for me) is that the ‘types’ in the AIC are not translated Directly into C# – they are interfaces, meaning the standard for C# is to start with an ‘I.’ Also each AIC must be implemented explicitly in c#.
Does the following help?
and then: