Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

What is Parcelable.Creator<Options> in xamarin.android?

lescperlescper Member ✭✭✭✭

I find in java there is a Parcelable.Creator and some class extends from it.

public class OptionsCreator :Parcelable.Creator<Options>{}

what is it in xamarin.android? there is no Parcelable.Creator.

Tagged:

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    For this, you can refer to my sample code used in my app:

    1.create class MyParcelableCreator

        public sealed class MyParcelableCreator<T> : Java.Lang.Object, IParcelableCreator
          where T : Java.Lang.Object, new()
        {
            private readonly Func<Parcel, T> _createMethod;
    
            public MyParcelableCreator(Func<Parcel, T> createMethod)
            {
                _createMethod = createMethod;
            }
    
            public Java.Lang.Object CreateFromParcel(Parcel source)
            {
                return _createMethod(source);
            }
    
            public Java.Lang.Object[] NewArray(int size)
            {
                return new T[size];
            }
        }
    

    2.A usage example in model FriendlyMessage

       class FriendlyMessage : Java.Lang.Object, IParcelable
        {
            public String text; //messsge content
            public String name; // message sender
            public String photoUrl; // photo
    
            public String UId;
            public void setUid(String Id) {
                this.UId = Id;
            }
            public String getUId() {
                return UId;
            }
    
            private static readonly MyParcelableCreator<FriendlyMessage> _create = new MyParcelableCreator<FriendlyMessage>(GetMessage);
    
            [ExportField("CREATOR")]
            public static MyParcelableCreator<FriendlyMessage> Create()
            {
                return _create;
            }
    
            private static FriendlyMessage GetMessage(Parcel parcel)
            {
                FriendlyMessage msg = new FriendlyMessage();
                msg.text = parcel.ReadString();
                msg.name = parcel.ReadString();
                msg.photoUrl = parcel.ReadString();
                msg.UId = parcel.ReadString();
                return msg;
            }
            public FriendlyMessage()
            {
            }
    
            public FriendlyMessage(String text, String name, String photoUrl)
            {
                this.text = text;
                this.name = name;
                this.photoUrl = photoUrl;
                this.UId = Guid.NewGuid().ToString();
            }
    
            public String getText()
            {
                return text;
            }
    
            public void setText(String text)
            {
                this.text = text;
            }
    
            public String getName()
            {
                return name;
            }
    
            public void setName(String name)
            {
                this.name = name;
            }
    
            public String getPhotoUrl()
            {
                return photoUrl;
            }
    
            public void setPhotoUrl(String photoUrl)
            {
                this.photoUrl = photoUrl;
            }
    
            public int DescribeContents()
            {
                throw new NotImplementedException();
            }
    
            public void WriteToParcel(Parcel dest, [GeneratedEnum] ParcelableWriteFlags flags)
            {
                dest.WriteString(text);
                dest.WriteString(name);
                dest.WriteString(photoUrl);
                dest.WriteString(UId);
            }
    
    
            public override string ToString()
            {
                return "name = " + name +" text = " + text + " photoUrl= " + photoUrl+ " UId = " + UId;
            }
        }
    
  • lescperlescper Member ✭✭✭✭

    @jezh in the jd-gui I find that the java code is not
    public Java.Lang.Object CreateFromParcel(Parcel source) { return _createMethod(source); }

    but

     public **Other type class which inherit from Java.Lang.Object** CreateFromParcel(Parcel source)
            {
                return _createMethod(source);
            }
    

    then should it be java.lang.object be the type of the method?

  • jezhjezh Member, Xamarin Team Xamurai

    We know we usually need to serialize our model(e.g. the class FriendlyMessage) ,which also need to extend Java.Lang.Object and implement interface IParcelable.

     class FriendlyMessage : Java.Lang.Object, IParcelable
        {
            public String text; //messsge content
            public String name; // message sender
            public String photoUrl; // photo
    
            public String UId;
            public void setUid(String Id) {
                this.UId = Id;
            }
            public String getUId() {
                return UId;
            }
    
            private static readonly MyParcelableCreator<FriendlyMessage> _create = new MyParcelableCreator<FriendlyMessage>(GetMessage);
    
    
     // other code
    
      }
    

    When we set the type of function CreateFromParcel to Java.Lang.Object , then we can use any our custom model for class MyParcelableCreator.

    Note: In xamarin android , we need to extends Java.Lang.Object when we using interface IParcelable . This is different from Android usage. Please refer to the code of FriendlyMessage.

    And there is an article about this , you can check it here: https://damienaicheh.github.io/xamarin/xamarin.android/2018/05/26/xamarin-android-parcelable-en.html

    You will find all the source code here: https://github.com/damienaicheh/XamarinAndroidParcelable

  • lescperlescper Member ✭✭✭✭

    If I make a class extends from Java.Lang.Object and change the return type to the class there will be a error.

  • jezhjezh Member, Xamarin Team Xamurai

    Could you please post your code snippets?

  • jezhjezh Member, Xamarin Team Xamurai

    Hi @lescper , any updates?

  • lescperlescper Member ✭✭✭✭
  • jezhjezh Member, Xamarin Team Xamurai

    If I make a class extends from Java.Lang.Object and change the return type to the class there will be a error.

    Sorry, I didn't find your class that extends from Java.Lang.Object in your app.
    Can you elaborate on that?

Sign In or Register to comment.