Use/serialize data of Android Parcelable interface, androidparcelable

Source: Internet
Author: User

Use/serialize data of Android Parcelable interface, androidparcelable

First, we need to know that there are two Android serialization methods. One is to implement Serializable, which is an interface supported in JavaSE, later, google launched the Android-specific interface Parcelable (its performance is nearly 10 times faster than Serializable)

Then we need to know the basic method for passing instances in Android: Bundle. putSerializable (Key, Object), and Bundle. putParcelable (Key, Object), which must implement the Serializable and Parcelable interfaces respectively. Note that Parcelable cannot be used to store data on disks, because Parcelable cannot guarantee data continuity in the case of external changes. In this case, it is better to use Serializable.

Let's first look at the definition of the Parcelable interface:

1 public interface Parcelable 2 {3 // content description interface, basically no need to worry about 4 public int describeContents (); 5 // write interface function, Package 6 public void writeToParcel (Parcel dest, int flags); 7 // read interface, which is used to construct an instance that implements Parcelable from Parcel. Because the implementation class is unknown here, you need to use the template method to pass in the inherited class name through the template parameter 8 // in order to implement the template parameter input, here the Creator embedded interface is defined, contains two interface functions to return a single and multiple inherited class instances. 9 public interface Creator <T> 10 {11 public T createFromParcel (Parcel source ); 12 public T [] newArray (int size); 13} 14 public interface ClassLoaderCreator <T> extends Creator <T> 15 {16 public T createFromParcel (Parcel source, ClassLoader loader ); 17} 18}

After inheriting this interface, we mainly rewrite writeToParcel () to serialize data into a Parcel object.

The following describes how to implement a User class.Specific source code of Parcelable

1 package com.catvideo. TV .catvideo.com. catvideo. user; 2 3 import android. OS. parcel; 4 import android. OS. parcelable; 5 6/** 7 * @ author SVC 957109587@qq.com 8 * @ version 2016 �� 5 �� 31 �� 12:09:20 9 * @ Description 10 */11 public class User implements Parcelable {12 int userId; 13 private String userName; 14 private String passWord; 15 private String email; 16 boolean state; 17 18 public User () {19 20} 21 22 public User (String email, String passWord) {23 this. email = email; 24 this. passWord = passWord; 25} 26 27 public User (int userId, String userName, String passWord, boolean state) {28 this. userId = userId; 29 this. userName = userName; 30 this. passWord = passWord; 31 this. state = state; 32} 33 34 public User (int userId, String userName, String passWord) {35 this. userId = userId; 36 this. userName = userName; 37 this. passWord = passWord; 38 this. state = false; 39} 40 41 public int getUserId () {42 return userId; 43} 44 45 public void setUserId (int userId) {46 this. userId = userId; 47} 48 49 public String getUserName () {50 return userName; 51} 52 53 public void setUserName (String userName) {54 this. userName = userName; 55} 56 57 public String getPassWord () {58 return passWord; 59} 60 61 public void setPassWord (String passWord) {62 this. passWord = passWord; 63} 64 65 66 public String getEmail () {67 return email; 68} 69 70 public void setEmail (String email) {71 this. email = email; 72} 73 74 public boolean isState () {75 return state; 76} 77 78 public void setState (boolean state) {79 this. state = state; 80} 81 82 @ Override 83 public int describeContents () {84 return 0; 85} 86 87 @ Override 88 public void writeToParcel (Parcel dest, int flags) {89 boolean [] states = {state}; 90 dest. writeString (userName); 91 dest. writeInt (getUserId (); 92 dest. writeBooleanArray (states); 93} 94 95 // public static final Parcelable. creator <User> CREATOR = new Parcelable. creator <User> () {96 // rewrite Creator 97 // 98 // @ Override 99 // public User createFromParcel (Parcel source) {100 // User user = new User (); 101 // user. userName = source. readString () 102 // 103 // p. map = source. readHashMap (HashMap. class. getClassLoader (); 104 // p. name = source. readString (); 105 // return p; 106 //} 107 // 108 // @ Override109 // public User [] newArray (int size) {110 // TODO Auto-generated method stub111 // return null; 112 //} 113 //}; 114}

 

You can use it as follows:

intent.putExtra("user", user);

Or use Bundle. putParcelable (Key, Object)

 

SerializableImplementation andParcelabelImplementation differences

1) To implement Serializable, you only need to implement implements Serializable without any implementation methods. This only marks the object and the system will automatically serialize it.

2) The implementation of Parcelabel requires not only implements Parcelabel, but also adding a static member variable CREATOR to the class. This variable must implement the Parcelable. Creator interface.

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.