Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a complex object through an intent in Android

I have an object which has the following class fields:

int, int, String, MyDatabaseType (custom object), List < MyDatabaseDetail > (array list of custom objects)

Is it possible for me to pass this through an intent/bundle?

I've played around a little bit with serializable and parcelable, but I couldn't get it working. Would I need to make all of custom object types parcelable, instead of just the main one that I want to pass?

Surely there is a better way?

like image 940
b85411 Avatar asked Oct 02 '22 12:10

b85411


1 Answers

When you pass data in an intent, it must be Parcelable. That is because the intent may passed to a different application, and thus a different VM. As your object may be crossing process boundaries, it needs a mechanism that will allow it to be saved/restored. This is analogous to passing data via a web service call (in this case, the object is "flattened" to something like either XML or JSON).

Even if the intent stays within your application, parcelability allows the object to survive even if Android chooses to kill/re-launch your application, which can happen if memory is running low.

Serialization can be used instead of parcelization, however parcelization is more efficient.

like image 76
EJK Avatar answered Oct 10 '22 03:10

EJK