Two methods of Android serialization __android

Source: Internet
Author: User

In Android development, it is often necessary to transfer parameters, from one activity to another, or from activity to service, and the system itself supports some Java base type delivery, but complex custom types, It needs to be serialized before it can be passed. Serializable

The public class book implements Serializable {

    //This argument prevents deserialization failure when a class parameter changes. Because the system verifies that the Serialversionuid value is the same when deserializing,
    //If it is not the same, deserialization fails. When we do not specify the Serialversionuid value, the system is automatically generated based on the hash value of the parameter.
    //When the parameters are changed, the system generated hash value will change. Causes deserialization to fail.
    private static final long serialversionuid = 1L;

    Private String name = "";
    private int price;

    Public String GetName () {return
        name;
    }

    public void SetName (String name) {
        this.name = name;
    }

    public int GetPrice () {return price
        ;
    }

    public void Setprice (int price) {
        This.price = Price;
    }
}

Serializable is an empty interface, which indicates that the class can be serialized.
The use of serializable to achieve serialization is relatively simple, just need to serialize the entity class to implement the Serializable interface, you can. This approach is simple to implement, but is less efficient and creates many zero objects. pacelable

Public class people implements parcelable {private String name;
    private int age;
    Private BOOK2 book = new Book2 ();

    Private list<book2> book2list = new arraylist<> ();
        Public people (Parcel Parcel) {name = Parcel.readstring ();
        Age = Parcel.readint ();
        Book = parcel.readparcelable (Book2.class.getClassLoader ());
        parcelable[] Pars = Parcel.readparcelablearray (Book2.class.getClassLoader ());
    Book2list = Arrays.aslist (arrays.aslist (pars). ToArray (new book2[pars.length));
    Public String GetName () {return name;
    public void SetName (String name) {this.name = name;
    public int getage () {return age;
    public void Setage (int age) {this.age = age; public static final creator<people> Creator = new creator<people> () {@Override public
        People Createfromparcel (Parcel in) {Return to New people (in);

  }      @Override public people[] NewArray (int size) {return new people[size];

    }
    };
    @Override public int describecontents () {return 0;
        @Override public void Writetoparcel (Parcel dest, int flags) {dest.writestring (name);
        Dest.writeint (age);
        Dest.writeparcelable (book, flags);
    Dest.writeparcelablearray (Book2list.toarray (New Book2[book2list.size ()), flags); }
}

Implementing the Pacelable interface for serialization is complicated by writing the parameters that need to be serialized in order in the Writetoparcel method, defining a construction method with parcel parameters, and then taking the parameters out of the parcel in turn. The
is more efficient with pacelable, but it cannot be used for persistence and can only be used for serialization. It is unique to Android, and serializable is generally available in JRE.

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.