On the serialization of Android _android

Source: Internet
Author: User
Tags int size serialization

Cause of serialization

The reasons for serialization can be summed up in the following three situations:

1. Permanently save the object, save the object's byte sequence to the local file;
2. The object is transmitted in the network;
3. Objects are passed between IPC.

Serialization method

In the Android system, there are generally two methods for serialization, namely, implementing the Serializable interface and the Parcelable interface, where the Serializable interface is a serialized interface from Java, The parcelable is a serialized interface with Android.

Both of these serialization interfaces have their own advantages and disadvantages, and we need to use them for different situations.


1.Serializable generates a large number of temporary variables when serialized, resulting in frequent GC, compared to parcelable performance (after all, Android), so when using memory ( For example, serializing an object in a network to pass objects or serialize objects between processes, it is more recommended to use the Parcelable interface.

2. But Parcelable has an obvious drawback: you cannot use a situation where you want to store data on disk (for example, permanently saving objects, storing byte sequences of objects into local files), because parcel is not a common serialization mechanism for better implementation of objects among IPC. When changing the underlying implementation of any parcel data may result in previous data being unreadable, it is recommended that serializable be used at this time.

Code implementation

Implementation and application of Serializable interface

Serializable interface implementation is very simple, only need to serialize the class to inherit serializable, the system will automatically serialize it, the specific code is as follows:

public class Book implements Serializable {
  private static final long serialversionuid = 21455356667888L;
  Private String mname;
  Private String Mprice;

  Public String Getmname () {return
    mname;
  }

  public void Setmname (String mname) {
    this.mname = mname;
  }

  Public String Getmprice () {return
    mprice;
  }

  public void Setmprice (String mprice) {
    this.mprice = Mprice;
  }

}

How to use in activity:

Serializable Object delivery method public
void Setserializablemethod () {book Book
  = new book ();
  Book.setmname ("Wang Haikang");
  Book.setmprice ("20$");
  Intent Intent = new Intent (this, booktest.class);
  Bundle Bundle = new Bundle ();
  Bundle.putserializable (Ser_key, book);
  Intent.putextras (bundle);
  StartActivity (intent);
}

Serializable object Fetch method public book
Getserializablemethod () {book
  Mbook = (book) getintent (). Getserializableextra (Ser_key);
  return mbook;
}

Implementation and application of Parcelable interface

The implementation of Parcelable interface can be divided into a few steps:
1) implements Parcelable.
2 Rewrite the Writetoparcel method to serialize your object into a parcel object, that is, to write the data of the class to an externally provided parcel, to package the data that needs to be passed to the parcel container to save the data from the parcel container.
3 Rewrite the Describecontents method, the Content interface description, the default return 0 can be.
(4) Instantiate the static internal object Creator Implement Interface Parcelable.creator.
Note: If you think of parcel as a stream, you first write the object into the stream through the Writetoparcel, and then read the object from the stream by Createfromparcel, so the write order and read Order of the class implementation must be the same.
The specific implementation code is as follows:

public class Person implements parcelable {private String mname;
  Private String Msex;

  private int mAge;
  Public String Getmname () {return mname;
  } public void Setmname (String mname) {this.mname = Mname;
  Public String Getmsex () {return msex;
  } public void Setmsex (String msex) {this.msex = Msex;
  public int getmage () {return mAge;
  The public void setmage (int mAge) {this.mage = MAge;
  @Override public int describecontents () {return 0;
    @Override public void Writetoparcel (Parcel dest, int flags) {dest.writestring (mname);
    Dest.writestring (Msex);
  Dest.writeint (MAge); public static final parcelable.creator<person> Creator = new creator<person> () {@Override publi
      C Person Createfromparcel (Parcel source) {Person person = new person ();
      Person.mname = Source.readstring ();
      Person.msex = Source.readstring ();
      Person.mage = Source.readint (); ReTurn person;
    @Override public person[] NewArray (int size) {return new person[size] to be invoked when deserializing this class array;

 }
  };

How to use in activity:

1) Passing a single object, the specific code is as follows:

Parcelable Object Delivery method public
void Setparcelablemethod () {Person of person
  = new person ();
  Person.setmname ("Wang Haikang");
  Person.setmsex ("male");
  Person.setmage ();
  Intent Intent = new Intent (this, persontest.class);
  Bundle Bundle = new Bundle ();
  Bundle.putparcelable (Par_key, person);
  Intent.putextras (bundle);
  StartActivity (intent);
}

Parcelable Object Get method public person
Getparcelablemethod () {person
  Mperson = (person) getintent (). Getparcelableextra (Par_key);
  return Mperson;
}

2) Pass the object list, the specific code is as follows:
Note that if the list personlist = new ArrayList (), the error occurs because one of the parameters of the Putparcelablearraylist () function called below is of type ArrayList.

Parcelable Object List delivery method public
void Setparcelablelistmethod () {
  arraylist<person> personlist = new Arraylist<person> ();
  Person Person1 = new person ();
  Person1.setmname ("Wang Haikang");
  Person1.setmsex ("male");
  Person1.setmage ();
  Personlist.add (Person1);
  Person Person2 = new person ();
  Person2.setmname ("Xueyue");
  Person2.setmsex ("male");
  Person2.setmage ();
  Personlist.add (Person2);
  Intent Intent = new Intent (this, persontest.class);
  Bundle Bundle = new Bundle ();
  Bundle.putparcelablearraylist (Par_list_key, personlist);
  Intent.putextras (bundle);
  StartActivity (intent);
}

Parcelable Object Fetch method public
arraylist<person> Getparcelablemethod () {
  arraylist<person> Mpersonlist = Getintent (). Getparcelableextra (Par_list_key);
return mpersonlist;
}

3 finally introduces an opportunistic method:
The delivery of objects in IPC can be achieved without inheriting parcelable or serializable methods. The implementation principle of this method is not very clear, only know the code in the new ArrayList () The return is actually a emptyarray.object array, but I feel that the system call serializable should be serialized, if you have good ideas, welcome to inform.
The specific code is as follows:

Object list passes public
void Setobjectmethod () {
  ... (omitted)
  ArrayList list = new ArrayList ();
  ObjectList is a list of objects
  List.add (ObjectList);
  Bundle.putparcelablearraylist (Par_list_key, LIST);
  Intent.putextras (bundle);
  StartActivity (intent);
}

Gets the object list
ArrayList list = bundle.getparcelablearraylist ("list");
Turn it into your own definition of the list, so ObjectList is the list you pass over.
objectlist= (list<object>) list.get (0);

Related Article

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.