Pass Objects between Activities

Heinz Wäscher 1 Reputation point
2021-07-03T08:50:38.147+00:00

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 www.DeepL.com/Translator (free version)

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

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,936 Reputation points
    2021-07-07T08:48:44.697+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

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

       //Activity1  
       Intent intent = new Intent(this, typeof(Activity1));  
       TestModel model = new TestModel { Name = "test_name" };  
       intent.PutExtra("model", JsonConvert.SerializeObject(model));  
       StartActivity(intent);  
    
       //Activity2  
       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.

       [Application]  
       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" ...>  
         </application>  
       </manifest>  
    
    
       //Activity1  
       var model = CustomApplication.TheModel;  
       model.Name = "testing...";  
       StartActivity(new Intent(this, typeof(Activity1)));  
    
       //Activity2  
       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.