Hi everyone!
I am exercising my android knowledge. I met the Parcelable before but now i have more questions. As we know with Parcelable can we pass datas between activities. Right now the SharedPreferences and other data storing procedures don't count. The serializable does. That is the other way to pass data between activites? Which is the better (Parcelable vs. Serializable)?
Generally we handle the datas as extras. We put the data in the intent with
Intent subActivity = new Intent(this, typeof(SubActivity));
subActivity.PutExtra("dogName", "Fluffy");
and the other side we get the data with
string name = Intent.GetStringExtra("dogName");
The dogName is the identifier to get the correct data if we specified more.
But this working so simple only with atomic types(string, int, float, short, etc.). When we want to pass complex data, a Dog object with name, age, etc. then we need a class.
A data container class creation is very simple. We create the fields, the properties to get or set them, and for example a constructor to set them at the beginning. But to pass them is not so easy.
1) First step is to implement the IParcelable interface. Ok this is not so difficult because we do this before more time for example by IOnClickListener. The compiler warn us to implement the missing methods.
Let me ask a question: Is it possible to autogenerate these methods? When i must implement them i need to search on the internet how those methods looks like(parameters, return type, public/private/protected, the name is available in the error message).
2) Ok, i implemented the missing methods( DescribeContents, WriteToParcel). But this is not enough. As i saw i need to specify a static InitializeCreator method:
[ExportField ("CREATOR")]
public static DogsCreator InititalizeCreator()
{
return new DogsCreator ();
}
For this ParcelableCreator, named "DogsCreator" i need an another class. Implementing an interface (IParelableCreator) again.
Ok it is not so difficult because the compiler warned me about this two methods and these are understandable. But i think this is so complex.
3) I like to use it as with the atomic types:
Dog dog = new Dog("Fluffy", 12);
Intent subActivity = new Intent(this, typeof(SubActivity));
subActivity.PutExtra("fluffyDog", dog);
StartActivity(subActivity);
In SubActivity.cs
Dog dog = Intent.GetParcelableExtra("dog") as Dog;
This method is understandable too. The "Dog" is not a string, int or float, it is an object. The GetObjectExtra would be better name but ok the GetParcelableExtra is good too. It is easly notable.
I could reach that to pass object between activites but what about to pass object list? I cannot do that:
List<Dog> dogies = new List<Dog>();
Intent subActivity = new Intent(this,typeof(SubActivity));
subActivity.PutParcelableArrayListExtra("puppies", dogies);
StartActivity(subActivity);
In the SubActivity.cs
List<Dog> puppies = Intent.GetParcelableArrayListExtra("puppies");
This one is not working.
Can someone show me a working example for this in Xamarin?
Is this a simplier method to pass datas (atomic types, objects, list of objects) between activities as i presented above?
Huge thanks in advance.