-1


I want to know when to use parcelable and when not to . I know that parcelable is way of parcel complex data type in android , but as per official document http://developer.android.com/guide/faq/framework.html#3 , nothing such is mention . So when is parcelable really needed ??

Code_Life
  • 5,742
  • 4
  • 29
  • 49

2 Answers2

0

By implementing the Parcelable interface, you can make your class object capable of being stored in a Bundle and you can then easily pass it to another activity with the help of an Intent.

e.g.

Intent i = new Intent(....);
i.putParcelableExtra("name", object);

Then in the other activity, get it like this:

YourClass object = (YourClass) getIntent().getExtras().getParcelableExtra("name");

Notice the typecasting. That is necessary in order to use your class's methods.

Basant Singh
  • 5,736
  • 2
  • 28
  • 49
  • yes i agree , but my point is what is the need of this when u can simply pass the object via static method just like normal java . – Code_Life Nov 13 '13 at 05:46
  • Static methods may do the work for you, but they are not advised in this situation. This is the common programming pattern while passing objects between activities using intents, because intents are actually made to do that task. And like @kalyan said, using static methods may increase memory usage. – Basant Singh Nov 13 '13 at 05:52
  • @MohitSharma You should read about object's variable and class variables first. Read differences. This is the main thing of your confusion... – Pankaj Kumar Nov 13 '13 at 05:54
  • @basant_matharu : I totally agree with you that this is Android Programming not java programming , so there will be few thing which we will doing differently , but i couldn't find single official document suggesting me to do so .. check the link i have shared once ... , yes static method increase memory usage but creating parcelable will add load to my App (much lesser than serialization) – Code_Life Nov 13 '13 at 07:35
  • @MohitSharma You are right, Parcelable is good if you are passing small objects.. – Basant Singh Nov 13 '13 at 08:39
0

To pass data to another activity, we are usually put bundle object on activity intent that will be called. Bundle can be filled with primitive data types like long, integer, and boolean. It can be filled with simple data type like String class to represent text. For example, an activity calls another activity at the same time sends simple data to it.

In destination activity, we check the bundle. If it is exist, we open the data of bundle from the origin activity. Now, how if we want to pass the complex data type like our defined class object to another activity? For this need, we can use Parcelable in Android.

Parcelable is an interface for classes so a class that implements Parcelable can be written to and read from a Parcel. The data in Parcel form can be passed between two threads. Parcel itself is a class that have abilities to serialize and deserialize object of class.

Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36