As you may know when moving from one activity to another in android you cannot simply pass an object by reference. To get around this you can store a reference of your object in a static variable but global type variables are generally considered anti-patterns. A better approach is to serialize the object and receive a copy of it in the target activity. This can be done a variety of ways such as implementing Java.IO.ISerializable, json serialization, etc. Doing this can lead to varying levels of work on your part.
To serialize and send a copy on a CSLA object from an Activity that inherits from the Csla ActivityBase, use the SeirializeModelForParameter method:
var projectResourceListActivity = new Intent(this, typeof(ProjectResourceList));
projectResourceListActivity.PutExtra(Constants.EditParameter, this.SerilizeModelForParameter());
StartActivity(projectResourceListActivity);
This method takes whatever is in the current view model's model property and serializes it using the mobile formatter. If you need to send along a different object you can simply pass a reference to it as a parameter in the SerializeModelForParameter(objectIWantToSerialize) command instead.
To reconstitute the object on the target activity you can use a different method on the activity, DeserializeFromParameter. A good place to do that might be in the activity's OnCreate event:
Library.ProjectEdit projectEdit;
var serializedProjectEdit = Intent.GetByteArrayExtra(Constants.EditParameter);
if (serializedProjectEdit != null)
{
projectEdit = (Library.ProjectEdit)this.DeserializeFromParameter(serializedProjectEdit);
}
That's all their is too it!
No comments:
Post a Comment