Serializing an Object for a New Action

I am coding in Xamarin and I need to pass the object to another action, however I get an error message regarding the conversion of the object to "Android.OS.Bundle"

Here are the errors:

Best overloaded method matching 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 that I use to pass an object to another action:

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

Can I get some help to make this code work?

Thank you in advance

+4
1

. , . "Singleton pattern" - , , . , . ( , - , ). "MainActivity" - :

var testObj = new TestObjectToSerialize();
testObj.testString = "testString";
var formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, testObj);

var secondActivity = new Intent(this, typeof(SecondActivity));
secondActivity.PutExtra("testObj", stream.ToArray());
StartActivity(secondActivity);

SecondActivity , :

var byteArr = Intent.GetByteArrayExtra("testObj");
var stream = new MemoryStream(byteArr);
var formatter = new BinaryFormatter();
var testObj = formatter.Deserialize(stream) as TestObjectToSerialize;

Toast.MakeText(this, testObj.testString, ToastLength.Long).Show();

. Java.IO.ISerializable. Mono.Android.Export.dll . TestObjectToSerialize , :

using Java.Interop;

:

public class TestObjectToSerialize : Java.Lang.Object, Java.IO.ISerializable
{
    public string testString { set; get; }

    //The magic starts here

    public TestObjectToSerialize()
    {
        Console.WriteLine("TestObjectToSerialize..ctor");
    }

    //You have to make this one
    public TestObjectToSerialize(IntPtr handle, JniHandleOwnership transfer)
        : base(handle, transfer)
    {
        Console.WriteLine("TestObjectToSerialize..ctor(IntPtr, JniHandleOwnership)");
    }

    [Export("readObject", Throws = new[] {
    typeof (Java.IO.IOException),
    typeof (Java.Lang.ClassNotFoundException)})]
    private void ReadObjectDummy(Java.IO.ObjectInputStream source)
    {
        testString = ReadNullableString(source);
    }

    [Export("writeObject", Throws = new[] {
    typeof (Java.IO.IOException),
    typeof (Java.Lang.ClassNotFoundException)})]
    private void WriteObjectDummy(Java.IO.ObjectOutputStream destination)
    {
        WriteNullableString(destination, testString);
    }

    static void WriteNullableString(Java.IO.ObjectOutputStream dest, string value)
    {
        dest.WriteBoolean(value != null);
        if (value != null)
            dest.WriteUTF(value);
    }

    static string ReadNullableString(Java.IO.ObjectInputStream source)
    {
        if (source.ReadBoolean())
            return source.ReadUTF();
        return null;
    }
}

MainActivity :

var testObj = new TestObjectToSerialize();
testObj.testString = "testString";

var secondActivity = new Intent(this, typeof(SecondActivity));
secondActivity.PutExtra("testObj", testObj);
StartActivity(secondActivity);

SecondActivity :

var testObj = Intent.GetSerializableExtra("testObj") as TestObjectToSerialize;
Toast.MakeText(this, testObj.testString, ToastLength.Long).Show();

=)

0

Source: https://habr.com/ru/post/1524006/


All Articles