In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to use the Parcelable interface in Android". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Detailed explanation of how to use Android Parcelable interface
1. Parcelable interface
Interface for classes whose instances can be written to and restored from a Parcel . Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface .
two。 Parcelable is implemented for serialization, so why serialize?
1) permanently save the object and save the byte sequence of the object to the local file
2) pass objects in the network by serializing them
3) passing objects between processes through serialization.
3. The method of implementing serialization
There are two options for 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 (a unique function of android, which is more efficient than the Serializable interface, and can be used for Intent data transfer and interprocess communication (IPC)). Implementing the Serializable interface is very simple and can be declared, while implementing the Parcelable interface is slightly more complex but more efficient, and it is recommended to improve performance.
Note: there are two ways to pass Intent objects in Android: one is Bundle.putSerializable (Key,Object), and the other is Bundle.putParcelable (Key,Object). Of course, there are certain conditions for these Object. The former implements the Serializable interface, while the latter implements the Parcelable interface.
4. Principles for choosing a serialization method
1) when using memory, Parcelable has higher performance than Serializable, so Parcelable is recommended.
2) Serializable generates a large number of temporary variables during serialization, resulting in frequent GC.
3) Parcelable cannot be used when the data is to be stored on disk, because Parcelable cannot ensure the persistence of the data in the event of external changes. Although Serializable is less efficient, Serializable is recommended at this time.
5. Application scenario
Some data needs to be passed through Intent between multiple parts (Activity or Service). Simple types (such as numbers, strings) can be put directly into Intent. Complex types must implement the Parcelable interface.
6. Parcelable API definition
Public interface Parcelable {/ / content description interface, basically ignore public int describeContents (); / / write interface function, package public void writeToParcel (Parcel dest, int flags); / / read interface, the purpose is to construct an instance processing of a class that implements Parcelable from Parcel. Because the implementation class is still unknown here, we need to use the template method to pass in the inheritance class name through the template parameters / / in order to be able to pass in the template parameters, the Creator embedded interface is defined, which contains two interface functions that return single and multiple instances of the inherited class public interface Creator {public T createFromParcel (Parcel source); public T [] newArray (int size);}}
7. Implement the steps of Parcelable
1) implements Parcelable
2) override the writeToParcel method to serialize your object into a Parcel object, that is, write the data of the class to the externally provided Parcel, package the data to be passed and store it in the Parcel container, so as to obtain the data from the Parcel container
3) override the describeContents method and content API description. You can return 0 by default.
4) instantiate static internal object CREATOR to implement interface Parcelable.Creator
Public static final Parcelable.Creator CREATOR
Note: none of the public static final can be lost, and the name of the internal object CREATOR cannot be changed. It must be all capitalized. Two methods in this API need to be overridden: createFromParcel (Parcel in) reads the passed data value from the Parcel container and encapsulates it into a Parcelable object to return the logic layer. NewArray (int size) creates an array of type T and length size with only one sentence (return new T [size]) for external classes to deserialize the array of this class.
In short: map your objects to Parcel objects through writeToParcel, and then map Parcel objects to your objects through createFromParcel. You can also think of Parcel as a stream, writing objects to the stream through writeToParcel, and reading objects from the stream through createFromParcel, but this process requires you to implement, so the order of writing and reading must be the same.
The code is as follows:
Public class MyParcelable implements Parcelable {private int mData; public int describeContents () {return 0;} public void writeToParcel (Parcel out, int flags) {out.writeInt (mData);} public static final Parcelable.Creator CREATOR = new Parcelable.Creator () {public MyParcelable createFromParcel (Parcel in) {return new MyParcelable (in);} public MyParcelable [] newArray (int size) {return new MyParcelable [size];}} Private MyParcelable (Parcel in) {mData = in.readInt ();}}
8. The difference between Serializable implementation and Parcelabel implementation
1) only implements Serializable is needed to implement Serializable. This is just a tag to the object, and the system serializes it automatically.
2) the implementation of Parcelabel requires not only implements Parcelabel, but also a static member variable CREATOR in the class, which needs to implement the Parcelable.Creator interface.
Comparison of the two codes:
1) create a Person class to implement Serializable
Public class Person implements Serializable {private static final long serialVersionUID =-7060210544600464481L; private String name; private int age; 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;}}
2) create a Book class to implement Parcelable
Public class Book implements Parcelable {private String bookName; private String author; private int publishDate; public Book () {} public String getBookName () {return bookName;} public void setBookName (String bookName) {this.bookName = bookName;} public String getAuthor () {return author;} public void setAuthor (String author) {this.author = author;} public int getPublishDate () {return publishDate } public void setPublishDate (int publishDate) {this.publishDate = publishDate;} @ Override public int describeContents () {return 0;} @ Override public void writeToParcel (Parcel out, int flags) {out.writeString (bookName); out.writeString (author); out.writeInt (publishDate);} public static final Parcelable.Creator CREATOR = new Creator () {@ Override public Book [] newArray (int size) {return new Book [size] } @ Override public Book createFromParcel (Parcel in) {return new Book (in);}}; public Book (Parcel in) {bookName = in.readString (); author = in.readString (); publishDate = in.readInt ();}} "how to use the Parcelable interface in Android" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.