Android Studio Add parcelable serialization gadget (quickly improve development efficiency)

Source: Internet
Author: User
Tags object serialization

Android Studio Add parcelable serialization gadget (quickly improve development efficiency)

Android Studio is a development tool that Google has developed specifically for Android, where you can directly add some very useful development gadgets, explaining how to add these gadgets And to recommend a very useful object when passing, the object must be serialized to the interface parcelable gadget;

Here we introduce the two options for serialization in Android: One is to implement the Serializable interface (which is supported by the javase itself), the first is to implement the Parcelable interface (Android-specific features, efficiency than the serializable interface to achieve efficient , can be used for intent data transfer, or for interprocess communication (IPC). The implementation of the serializable interface is very simple, declare it, and the implementation of the Parcelable interface is slightly more complex, but more efficient, it is recommended that this method to improve performance.

It is also worth noting that the intent transitive object in Android should have two methods: one is bundle.putserializable (Key,object) and the other is Bundle.putparcelable (Key,object). Of course, these objects have certain conditions, the former is the implementation of the serializable interface, and the latter is the implementation of the Parcelable interface.

Well, after you've introduced the object serialization, let's look at how to add a gadget like this:

First step: Click Settings (Setting)

Step Two: Click Plugins, then click on the B Rowse repositories referred to in 2(browse the repository)


The third step: then in 1 refers to the input box you want to download the plug-in gadget, and then 2 refers to the number of downloads and user ratings, 3 refers to the installation, click on the 3 refers to the installation button, you can download the installation of this plugin. Here to tell you are, the plug-ins here are all the global Android developers can download, so we see a lot of 4 stars and 5 stars of the score, are very worthy to study, all right when you can Baidu to see what is doing, think interesting words can come down to study, Here are a lot of interesting small plug-ins, can help us to reduce a lot of unnecessary code, the following recommendations for the Parcelable small plug-in is convenient for everyone to serialize the object.


All right, here's how to install the widget, and we'll show you how to install the Parcelable widget in the next steps.

Enter parcelable in 1, in the following results there is an Android parcelable code generator, yes it is, it is very high score, close to 5 stars, indicating the performance ah what is still very worthy of recognition, and more than 80,000 people download. Then we can click on the download and install it, it will be used;



Note that after downloading the installation, you must restart the widget that Android Studio has just installed to use;

Then teach everyone how to use;

First step: To the right mouse button in the entity class that you passed, such as, click Generate ... (or direct shortcut key Alt+insert)


Click on the Generate ... After that, there is the following menu interface, click Parcelable, you can directly quickly make the object is to want to parcelable.



Then a window pops up, let's select the properties to serialize, and here's a full selection like this:


After clicking OK, the object will be parcelable, and it will automatically generate some code later, which is very convenient.

The following is a click on the Parcelable, the object is successfully serialized, which saves us a lot of time, and the object using the Parcelable interface to achieve serialization, transfer between the activity is very fast.

Package Com.iqtogether.qxueyou.activity;import android.os.parcel;import android.os.parcelable;/** * Created by Chengguo on 2016/3/15.    */public class User implements parcelable {private String userId;    Private String UserName;    Private String Usersex;    private int userage;    Public String GetUserId () {return userId;    } public void Setuserid (String userId) {this.userid = UserId;    } public String GetUserName () {return userName;    } public void Setusername (String userName) {this.username = UserName;    } public String Getusersex () {return usersex;    } public void Setusersex (String usersex) {this.usersex = Usersex;    } public int Getuserage () {return userage;    } public void Setuserage (int userage) {this.userage = Userage;    } public String Getuserhome () {return userhome;    } public void Setuserhome (String userhome) {this.userhome = Userhome; } Private String Userhome;    @Override public int describecontents () {return 0;        } @Override public void Writetoparcel (Parcel dest, int flags) {dest.writestring (This.userid);        Dest.writestring (This.username);        Dest.writestring (This.usersex);        Dest.writeint (This.userage);    Dest.writestring (This.userhome);        Public user () {} protected User (Parcel in) {This.userid = In.readstring ();        This.username = In.readstring ();        This.usersex = In.readstring ();        This.userage = In.readint ();    This.userhome = In.readstring (); public static final parcelable.creator<user> Creator = new parcelable.creator<user> () {public use        R Createfromparcel (Parcel source) {return new User (source);        } public user[] NewArray (int size) {return new user[size]; }    };}

Note: If the user object contains an object property, the object property itself must also implement the Parcelable interface, such as an object property of the user object, without the implementation of the Parcelable interface will be a failure to serialize the prompt;

This gives the user a House object property, and here's an example.


Then the error message is as follows:


tip : You are required to pass an object that implements the Parcelable interface. This time we go to the House object to implement the Parcelable interface is OK.

The following House objects:

Package Com.iqtogether.qxueyou.activity;import android.os.parcel;import android.os.parcelable;/** * Created by Chengguo on 2016/3/15.    */public Class House implements parcelable {private String price;    Public String GetPrice () {return price;    The public void Setprice (String price) {this.price = Price;    } @Override public int describecontents () {return 0;    } @Override public void Writetoparcel (Parcel dest, int flags) {dest.writestring (this.price);    Public house () {} Protected House (Parcel in) {This.price = In.readstring (); public static final parcelable.creator


The following is the code for the entire user object:

Package Com.iqtogether.qxueyou.activity;import android.os.parcel;import android.os.parcelable;/** * Created by Chengguo on 2016/3/15.    */public class User implements parcelable {private String userId;    Private String UserName;    Private String Usersex;    private int userage;    Private house Userhouse;    Public house Getuserhouse () {return userhouse;    } public void Setuserhouse (house userhouse) {this.userhouse = Userhouse;    } public String GetUserId () {return userId;    } public void Setuserid (String userId) {this.userid = UserId;    } public String GetUserName () {return userName;    } public void Setusername (String userName) {this.username = UserName;    } public String Getusersex () {return usersex;    } public void Setusersex (String usersex) {this.usersex = Usersex;    } public int Getuserage () {return userage; } public void Setuserage (int userage) {this.userage = Userage;    } public String Getuserhome () {return userhome;    } public void Setuserhome (String userhome) {this.userhome = Userhome;    } private String Userhome;    Public User () {} @Override public int describecontents () {return 0;        } @Override public void Writetoparcel (Parcel dest, int flags) {dest.writestring (This.userid);        Dest.writestring (This.username);        Dest.writestring (This.usersex);        Dest.writeint (This.userage);        Dest.writeparcelable (This.userhouse, flags);    Dest.writestring (This.userhome);        } protected User (Parcel in) {This.userid = In.readstring ();        This.username = In.readstring ();        This.usersex = In.readstring ();        This.userage = In.readint ();        This.userhouse = In.readparcelable (House.class.getClassLoader ());    This.userhome = In.readstring (); public static final creator<user> Creator = new creator<user> () {public UsEr createfromparcel (Parcel source) {return new User (source);        } public user[] NewArray (int size) {return new user[size]; }    };}

All right, this completes the addition and use of the pacelable serialization gadget, is not very convenient and fast! In the end, don't forget, intentthe Transitive object is using bundle.putparcelable (key,object) instead of bundle.putserializable (Key,object). The object is received with Getintent (). Getparcelableexter (key) instead of Getintent (). Getserializableextera (key); I wish you all a pleasant work.















Android Studio Add parcelable serialization gadget (quickly improve development efficiency)

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.