Pass Objects between Activities

Heinz Wäscher 1 Reputation point

Good morning,

I have two Activities and would like to pass an object instance between them.
The object is of type WebDavClient from a nugetpacket.

// Activity a:
Intent intent = new Intent(this, typeof(NewAcivity));
intent.PutExtra("Client", JsonConvert.SerializeObject(client));

// Activity b:
client = JsonConvert.DeserializeObject<WebDavClient>(Intent.GetStringExtra("Client"));

Am I on the wrong track in general or does it only not work with this object?
How else can objects be passed?
Via the class Intent?

Many thanks for your help.

Translated with (free version)

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,283 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,936 Reputation points


    Welcome to our Microsoft Q&A platform!

    1.Try using a custom object class as the tranfer parameter. Here the sample code, please check:

       Intent intent = new Intent(this, typeof(Activity1));  
       TestModel model = new TestModel { Name = "test_name" };  
       intent.PutExtra("model", JsonConvert.SerializeObject(model));  
       var model = JsonConvert.DeserializeObject<TestModel>(Intent.GetStringExtra("model"));  
       string text = model.Name;  
       //the model class  
       public class TestModel  
           public TestModel()  
           public string Name { get; set; }  

    If the above code works well, the issue should be due to the 'WebDavClient'.

    2.To share the client between activities, you could also create a global variable of the application. Create a custom Application class for the project and add a parameter in the custom class.

       public class CustomApplication : Application  
           protected CustomApplication(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)  
           public static TestModel TheModel = new TestModel();  
       //set the `android:name` to the custom application class  
       <manifest ...>  
         <application android:name=".CustomApplication" ...>  
       var model = CustomApplication.TheModel;  
       model.Name = "testing...";  
       StartActivity(new Intent(this, typeof(Activity1)));  
       string text = CustomApplication.TheModel.Name;  

    Best Regards,

    Jarvan Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.