An example of Android implementing Parcelable object serialization

Source: Internet
Author: User
Tags int size object serialization serialization

Bundle.putparcelable can implement passing objects, but the object's class must implement the Parcelable interface before it can be used.

Here is a simple example of passing an object between the activity.

Parcel realizes the serialization of parameters and serializes the class objects into homogeneous binary streams to improve the efficiency of transmission.

Parcelable class. The main class, which is the class of the object we are transferring, needs to implement the Parcelable interface.

 import Android.os.Parcel;    
        
Import android.os.Parcelable;    
        
    public class Benparcelable implements parcelable {public String name;    
        
    public int age;    
        
    public String profession;    
        Public benparcelable (string name, int age, string profession) {this.name = name;    
        This.age = age;    
    This.profession = profession;    
    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 String getprofession () {return profession;    
    } public void Setprofession (String profession) {this.profession = profession;
   } @Override public int describecontents () {return 0; @Override public void Writetoparcel (Parcel Parcel, int flag) {parcel.writestring (name)    
        ;    
        Parcel.writeint (age);    
    Parcel.writestring (profession);    
        public static final creator<benparcelable> Creator = new Creator<benparcelable> () {    
        Public benparcelable Createfromparcel (Parcel in) {Return to New benparcelable (in);    
        Benparcelable[] NewArray (int size) {return new benparcelable[size];    
        
    }    
    };    
        Private benparcelable (Parcel in) {name = In.readstring ();    
        Age = In.readint ();    
    profession = in.readstring (); }    
}

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.