Pass Objects between Activities

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)
Hi, HeinzWscher-4832. There is nothing wrong with the code. I created a basic demo to test the code, it works as expected. Is there any error occur? Try to create a custom object class to replace 'WebDavClient' to test the function to check if the issue is caused by the 'WebDavClient' class.
Hello alessandrocaliaro,
I get a string of length 2 containing {}.
That's all.
Hello JarvanZhang,
I tried to convert a Bitmap- and a Stream-Object via JsonConvert.SerializeObject().
In both cases I got Errors from JsonConvert.SerializeObject().
Could it be that only simple objects are serialisable through this function?
ok, you have no data... Are you sure that you object "client" in this line has some data?
The parameter of the 'JsonConvert.SerializeObject' method is type of object, which should work for all types. What error occured in your test? Please post more details about the error. And share the related code, it'll help to reproduce the problem.
Yes, I am sure because I used the object before.
I did a test and requested a certain fold/its content. Got it.
Passed the object to Activity2, made the the same test and got an Error.
In this case I don*t get an error message at all, it just does not work.
My Code:
// In Activty1:
// Init:
WebDavClientParams clientParams = new WebDavClientParams{...} // My User + Pass
WebDavClient client = new WebDavClient(Global.clientParams);
// Test:
PropfindResponse result = client.Propfind("https://MYFOLDERSADDRESS").Result;
if (result.IsSuccessful)
{
Toast.MakeText(this, "FOLDER FOUND.", ToastLength.Short).Show();
}
else
{
Toast.MakeText(this, "FOLDER NOT FOUND.", ToastLength.Short).Show();
}
// -> Output: Found.
// Switch to Activity2:
Intent intent = new Intent(this, typeof(ActivityFolderView));
intent.PutExtra("CLIENT", JsonConvert.SerializeObject(client));
StartActivity(intent);
// In Activty2:
// Get Data:
WebDavClient client = JsonConvert.DeserializeObject<WebDavClient>(Intent.GetStringExtra("CLIENT"));
// Test:
PropfindResponse result = client.Propfind("https://MYFOLDERSADDRESS").Result;
if (result.IsSuccessful)
{
Toast.MakeText(this, "FOLDER FOUND.", ToastLength.Short).Show();
}
else
{
Toast.MakeText(this, "FOLDER NOT FOUND.", ToastLength.Short).Show();
}
// -> Output: Not found.
As mentioned before I also tried Bitmap and Stream and with them I got Errors when trying to serialize.
How can I edit send messages?
A typo:
WebDavClient client = new WebDavClient(Global.clientParams);
should be
WebDavClient client = new WebDavClient(.clientParams);
Sorry.
A small rectification:
In this case I don't get Errors, it just does not work.
Sorry.
Sign in to comment
1 answer
Sort by: Most helpful
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:
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.
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.
Could you please insert for example a Bitmap-Object into your class and serialize it?
//the model class
public class TestModel
{
public Bitmap aBitmap = null;
public TestModel()
{
}
public string Name { get; set; }
}
And tell me your results?
Thank you.
I try to add a bitmap property to the object class and encountered the 'JsonSerializationException'. To fix this, we need to create a custom 'JsonConverter' class to convert the bitmap.
Hello.
That was the missing link.
I did not know that I have to "pre-format/convert" the Object.
Thank you very much for your effort and help.
Thank you.
My pleasure. Happy coding!
Sign in to comment
Activity