Forum Xamarin.Android

Object serialization for a new activity

I am coding in Xamarin and I need to pass an object to another activity, however I am getting an error in relation to converting the object to an 'Android.OS.Bundle'

Here are the errors:

The best overloaded method match for 'Android.Content.Intent.PutExtra(string, Android.OS.Bundle)' has some invalid arguments

Error CS1503: Argument 2: cannot convert from 'SimpleMapDemo.TestObjectToSerialize' to 'Android.OS.Bundle'

Here is the code I am using to pass the object to another activity:

TestObjectToSerialize testObjectToSerialize;
testObjectToSerialize.testString = "objectToSerialize";
Intent intent = new Intent (this.ApplicationContext, typeof(HomeScreen));
intent.PutExtra("objectToSerialize", testObjectToSerialize);

Here is the class:

[Serializable]
class TestObjectToSerialize
{
    public string testString{ set; get;}
}

May I please have some help to get this code working?

Thanks in advance

Answers

  • SKallSKall USMember ✭✭✭✭
    var bundle = new Bundle();
    bundle.PutSerializable("objectToSerialize", testObjectToSerialize);
    intent.PutExtras(bundle);
    
  • SimonCanningSimonCanning USMember

    Thank you for the code.

    I am getting the following errors though:

    Error CS1502: The best overloaded method match for 'Android.OS.Bundle.PutSerializable(string, Java.IO.ISerializable)' has some invalid arguments
    Error CS1503: Argument 2: cannot convert from 'SimpleMapDemo.TestObjectToSerialize' to 'Java.IO.ISerializable'
    
  • SKallSKall USMember ✭✭✭✭

    You will need to inherit the class from Java.IO.Serializable. The attribute you are using is from Mono, not from Java.

  • SimonCanningSimonCanning USMember
    edited January 2014

    I have inherited from that class, now I am getting the following errors:

    Error CS0535: 'SimpleMapDemo.TestObjectToSerialize' does not implement interface member 'Android.Runtime.IJavaObject.Handle'

    and

    Error CS0535: 'SimpleMapDemo.TestObjectToSerialize' does not implement interface member 'System.IDisposable.Dispose()'

Sign In or Register to comment.