2

I have a foreground service which I trigger on my Splash Activity

Intent StartServiceIntent;
StartServiceIntent = new Intent(this, typeof(PagesService));

StartServiceIntent.PutExtra("Table", LINKMODEL);

Log.Info("101028", "Connectted");
StartService(StartServiceIntent);

At point where I am using PutExtra, I want to send a data model items in a list List<LINKMODEL> to the service, the following data model

public class LINKMODEL
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public string Url { get; set; }       
    }

It seems like the service only accepts few parameters such as array etc,

How do I send a whole model to the service?

Ali
  • 2,702
  • 3
  • 32
  • 54

1 Answers1

2

The best way to do it is using Newtonsoft Json Nuget Package.

1.Install the Newtonsoft JSON package in case you don't have it

Install-Package Newtonsoft.Json -Version 11.0.1

2.When you have to send your object from one activity to another serialize your obj to JSON string.

string toSend = JsonConvert.SerializeObject(yourModelobj);

3.Pass it into the intent as a string

4.After receiving it on the other activity deserialize it and use it however you need.

LINKMODEL localDetails = JsonConvert.DeserializeObject<LINKMODEL>(intentData);

You can also do this on List<> obj's

Goodluck!

FreakyAli
  • 13,349
  • 3
  • 23
  • 63