7

I wanted to pass a Class Object from one activity to another in Xamarin.Android app. I can pass the simple strings using Intent.PutExtra method.

Does anybody know about it. anyhelp is appreciated :)

loop
  • 9,002
  • 10
  • 40
  • 76
  • You can use a component on the Xamarin store called Simple storage, will essentially do the hard work for you. https://components.xamarin.com/view/simple-storage – InitLipton Sep 26 '14 at 12:36

2 Answers2

13

Just adding in case someone else comes across this. The nice thing about Xamarin/.NET is how easy it is to use JSON. You can Serialize your data to a string and pass that through the Extras.

JSON.NET is a nice library (that you can find on the Xamarin component store) for this and there is also some built in JSON classes in .NET. An example using JSON.NET would be like this.

Intent i = new Intent(Application.Context, typeof(SecondActivity));
i.PutExtra("key", JsonConvert.SerializeObject(myObject));
StartActivity(i);

And in the other Activity you can deserialize it.

var obj = JsonConvert.DeserializeObject<OBJ_TYPE>(Intent.GetStringExtra("key"));

This is better than using a static reference in my opinion.

kevskree
  • 4,442
  • 3
  • 24
  • 32
  • thanks, it really didn't come to my mind :) but I need to pass object by reference :) – loop Oct 02 '14 at 20:55
3

The concept is the same as with a standard (non-Xamarin) application.

You can use Intent#putExtra(String, Parcelable) to pass any object that implements the Parcelable interface as an extra.

The Parcelable interface is a little bit complex, so be sure to read the documentation to ensure that your class conforms to the requirements. You may also want to check out this SO question for more information on creating a Parcelable class.

You cannot pass an object reference via an Intent. This is because Activities are designed to work completely independently of each other. Users can throw your Activity in the background while performing other tasks, so it is entirely possible (and very likely) that your Activity's variables will be garbage collected. When the user later comes back to your Activity, it should be able to recreate its state.

If you really need to pass a reference to an object directly, you can do so by making that object a static variable. While this is a quick and dirty way to solve the problem of getting data from one Activity to another, it does not solve the problem of the variable potentially being garbage collected at some point, and is generally a poor design choice.

Community
  • 1
  • 1
Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • Will Object be passed as a reference ? – loop Sep 25 '14 at 19:44
  • No. It will be bundled into a `Parcel`, and then restored from that `Parcel` when you retrieve it. – Bryan Herbst Sep 25 '14 at 19:45
  • How can I achieve this reference passing of object to another activity ? – loop Sep 25 '14 at 19:46
  • I recommend you don't. Activities are designed to run completely independently of each other. It is possible (and very likely) that the user will throw your Activity in the background while doing some other task, and will come back to it later. At that point, the object you were trying to maintain might have been garbage collected. If you REALLY need to, you could store the object statically and retrieve it in the new Activity. – Bryan Herbst Sep 25 '14 at 19:51
  • I am pretty new to android, you mean http://stackoverflow.com/questions/11733361/how-to-pass-reference-non-serializable-from-one-activity-to-another this way. – loop Sep 25 '14 at 19:54
  • Yes, that would be an example of passing a static variable. Be aware that this is still prone to your variable being garbage collected, so you need to have a backup plan in your second Activity if your static variable is null. – Bryan Herbst Sep 25 '14 at 19:56
  • Update you ans with this info I mark that right. but for now +1 – loop Sep 25 '14 at 20:01
  • @Tanis.7x why all examples of Parcelable implementation are Java? The idea of using Xamarin is to have all in C#, isn't it? – Ricardo stands with Ukraine Oct 15 '15 at 08:42
  • 2
    @Riga Because [Xamarin's documentation for Parcelable](https://developer.xamarin.com/api/type/Android.OS.Parcelable/) is just a copy-paste of Google's documentation, and Google's is typically better with regards to what is happening with the API. [Here's a blog post with a good Xamarin example](http://dan.clarke.name/2012/09/implementing-iparcelable-in-mono-for-android/). – Bryan Herbst Oct 15 '15 at 13:51
  • @Tanis.7x the link what you added is what I expected to find in Xamarin website. Thanks – Ricardo stands with Ukraine Oct 15 '15 at 14:28