Differences between Android -- serializable and parcelable

Source: Internet
Author: User

Http://www.cnblogs.com/Tammie/archive/2012/08/16/2642471.html

These two methods are used to serialize data. They are mainly used for data transmission when different activities communicate through intent.

I have never noticed this before, but putdata is used directly. For example:

Intent it=new Intent();Bundle bd=new Bundle();bd.putInt("aa",20);bd.putString("bb",ok);it.setExtras(bd);
it.setClass(Activity1.this.class,Activity2.this.class)

startActivity(it,Activity2.this.class);

In this way, another activity is started, and the data obtained in activity2 can be obtained:

Bundle bd1=(Bundle)getIntent().getExtras();String str=bd1.getString("bb");int ii=bd1.getInt("aa");

So when will serialization be used? I think it is similar to J/C #. If only some common structure data is transmitted, it only needs this method.

However, if a custom structure (such as a custom class) or nested (structure) data is transmitted, serialization is required. Otherwise, garbled characters cannot be parsed.

For example, to customize a data class book:

public Book {        private string bookname;        private string author;        Book(string s1,string s2){bookname=s1;author=s2}        public Setbookname(string s){bookname=s;}        public Setauthor(string s){author=s;}        public string getbookname(){return bookname ; }        public string getauthor(){return author;}                  }

Let two activities communicate through intent:

Intent it = new intent (); book b1 = New Book ("Dictionary", "anonymous"); it. setextras (B1); it. setclass (activity1.this. class, activity2.this. class) startactivity (it, activity2.this. class );
Book bd1=(Book)getIntent().getExtras();String str=bd1.getBookname();

String str1=bd1.getauthor();
 

This transmission is not transmitted by us. At this time, we need serialize.

Serialization reason:

1. Permanently Save the object and save the object's byte sequence to a local file;

2. Passing objects in the network by serializing objects;

3. objects are passed between processes through serialization.

However, both serializable and parcelable are serialized. When should I use it?

 

1. parcelable is superior to serializable in memory usage, so parcelable class is recommended.

2. serializable will generate a large number of temporary variables during serialization, resulting in frequent GC.

3. parcelable cannot be used when you want to store data on a disk, because parcelable cannot guarantee data continuity in the case of external changes. Although serializable is not recommended for low efficiency, we recommend that you use serializable in this case.

Implementation

1 serializable implementation, you only need to inherit implements serializable. This only marks the object and the system will automatically serialize it.

2. To implement parcelabel, you must add a static member variable creator to the class. This variable must inherit the parcelable. Creator interface.

 

public class MyParcelable implements Parcelable {     private int mData;     public int describeContents() {         return 0;     }     public void writeToParcel(Parcel out, int flags) {         out.writeInt(mData);     }     public static final Parcelable.Creator<MyParcelable> CREATOR             = new Parcelable.Creator<MyParcelable>() {         public MyParcelable createFromParcel(Parcel in) {             return new MyParcelable(in);         }         public MyParcelable[] newArray(int size) {             return new MyParcelable[size];         }     };          private MyParcelable(Parcel in) {         mData = in.readInt();     } }

 

Specific examples are recommended to see android_tutor example, http://blog.csdn.net/android_tutor/article/details/5740845

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.