Add a Parcelable serialization tool to Android Studio (rapidly improving development efficiency)

Source: Internet
Author: User
Tags object serialization

Add a Parcelable serialization tool to Android Studio (rapidly improving development efficiency)
Add a Parcelable serialization tool to Android Studio (rapidly improving development efficiency)

Android Studio is a development tool provided by google for Android development. You can directly add some useful development tools in it. here we will explain how to add these tools, we recommend a very useful object delivery interface Parcelable that must be serialized;

 

Here we will first introduce two options for implementing serialization in Android: one is to implement the Serializable interface (which is supported by JavaSE itself), and the other is to implement the Parcelable interface (which is a special feature of Android, the efficiency is more efficient than the Serializable interface. It can be used for Intent data transmission or inter-process communication (IPC )). The implementation of the Serializable interface is very simple. Just declare it. The implementation of the Parcelable interface is a little more complex, but more efficient. We recommend using this method to improve performance.

It is worth noting that the Intent object in Android should also have two methods: Bundle. putSerializable (Key, Object), and Bundle. putParcelable (Key, Object ). Of course, these objects have certain conditions. The former implements the Serializable interface, while the latter implements the Parcelable interface.

After introducing Object serialization, let's see how to add such a tool:

Step 1: Click Setting)

 

Step 2: Click Plugins, and then click Browse repositories (Browse repository)

 

Step 3: Collect the plug-in gadgets you want to download in the input box indicated by 1. Then 2 indicates the download quantity and user rating, and 3 indicates the installation, click the Install button in step 3 to download and install the plug-in. All the plug-ins here can be downloaded by Android Developers all over the world. Therefore, we can see that the scores of many 4 and 5 stars are worth studying, when it's okay, you can check what Baidu is doing. If you find it interesting, you can study it. There are many interesting small plug-ins that can help us reduce unnecessary code, the Parcelable plug-in we recommend below is convenient for Object serialization.

Now we have introduced how to install the Parcelable plug-in.

Enter Parcelable in step 1. In the following result, there is an Android Parcelable code generator. That's right. It scores very high, close to five stars, this shows that the performance is very positive, and there are more than 80 thousand downloads. Then we can click Download and install to use it. For example:

Note: After downloading and installing Android Studio, you must restart the plug-in just installed on Android Studio;

Next, we will teach you how to use it;

Step 1: Right-click the object class you passed, for example, click Generate... (or press the shortcut key Alt + Insert)

After clicking Generate..., the following menu interface appears. Click Parcelable to directly and quickly make the object want Parcelable.

Then a window will pop up. Let's select the property to be serialized. Here we select all of them as follows:

After clicking OK, this object implements Parcelable and some code will be automatically generated later, which is very convenient.

Next we click Parcelable, and the object will be serialized successfully. This saves us a lot of time, and the object is serialized using the Parcelable interface, which is very fast to transfer between activities.

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
 
   CREATOR = new Parcelable.Creator
  
   () {        public User createFromParcel(Parcel source) {            return new User(source);        }        public User[] newArray(int size) {            return new User[size];        }    };}
  
 

NOTE: IfA User object contains an object attribute. This object attribute must also implement the Parcelable interface, for example, an object attribute of a User object, if the Parcelable interface is not implemented, the serialization fails;

Set a property of the House object for the User. An example is provided here.

 

Then the error message is as follows:

 

Tip: you must pass an object that implements the Parcelable interface. At this time, we can implement the Parcelable interface for the House object.

The following House object:

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;    }    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
 
   CREATOR = new Parcelable.Creator
  
   () {        public House createFromParcel(Parcel source) {            return new House(source);        }        public House[] newArray(int size) {            return new House[size];        }    };}
  
 


The code for the entire User object is as follows:

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
 
   CREATOR = new Creator
  
   () {        public User createFromParcel(Parcel source) {            return new User(source);        }        public User[] newArray(int size) {            return new User[size];        }    };}
  
 
This completes the addition and use of the Pacelable serialization tool. Is it very convenient and convenient! In the end, do not forget that intent uses Bundle. putParcelable (Key, Object) instead of Bundle. putSerializable (Key, Object) when passing objects ). GetIntent (). getParcelableExter (Key) is used to receive objects, instead of getIntent (). getserializableexter (Key). have a good time working.

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.