Android Product Development (15)--Memory object serialization

Source: Internet
Author: User
Tags float double object serialization serialization definition

Reprint please indicate the source: a column of Maple Leaf

In the previous article we explained the Android app upgrade update operation, the app upgrade update operation is the standard of the app, upgrade is to get the app upgrade information, update operation is to download, install, update the app, where we can use the App Store to get the app's upgrade information , you can also get the app's upgrade information within the app by requesting the local server, and determine whether the app needs to be upgraded by comparing the version number of the local app.
Upgrade information is the basis of an app update, and only our app's upgrade information indicates that it needs to be updated before we can start a subsequent update – that is, download and install the Update app. Here is emphasized that the application of the upgrade operation is divided into ordinary upgrade and mandatory upgrade two, the normal upgrade operation is to complete an update operation of the app, and the mandatory update is the online app when the bug is a means of forcing users to upgrade, the user experience is not very good, So it is generally not recommended to use this method to upgrade the user's app.
For more information on app upgrade updates, refer to my: Android product development (14) –>app Upgrade and update

This article will explain the knowledge of data serialization that needs to be known in Android, and we know that transferring data between different activity during Android development can be passed through the put** method of the intent object, for the eight basic data types of Java (char int float Double long Short Boolean byte) there is no problem, but if you pass more complex object types (such as objects, such as collections, etc.), then there may be problems, and this introduces the concept of data serialization.

Definition of serialization:

Here, let's take a look. The definition of serialization on the encyclopedia

Serialization (serialization) transforms the state information of an object into a process that can be stored or transmitted in a form. During serialization, an object writes its current state to a temporary or persistent store. Later, the object can be recreated by reading or deserializing the state of the object from the store.

Simply put, our data needs to be transferred to the data that can be transmitted, and then in the transmission of the target can be deserialized to restore the data back, where the object state information into the data can be transferred to the process of serialization, the transfer of data can be transferred to the inverse of the process is deserialization.

Why serialization needs to be serialized:

Knowing the previous serialization definition, what does the memory object need to implement serialization?

    • Permanently saves the object, saving the object's byte sequence to a local file.

    • Objects are passed through the network in a serialized object.

    • Passes an object between processes through a serialized object.

Two ways to implement serialization:

So how do we get the serialization done? There are two ways in which we implement serialization in Android development:

    • Implementing the Serializable interface

    • Implementing the Parcelable Interface

The difference between the two serialization methods:

All know that there are two ways to serialize in Android Studio: Serializable and parcelable. So what's the difference between these two ways of serializing? Here are the differences between the two ways of implementing serialization:

    1. Serializeble is the way in which Java is serialized, and Parcelable is an Android-specific serialization method;

    2. When using memory, parcelable is better than serializable, so it is recommended to use parcelable.

    3. Serializable generates a large number of temporary variables at serialization time, resulting in frequent GC.

    4. Parcelable cannot be used in situations where data is stored on disk, because Parcelable does not guarantee the continuity of the data in the event of external change. Although serializable efficiency is low, it is not advocated, but in this case, it is recommended that you use serializable.

Finally there is a point is that the Serializeble serialization method is relatively simple, the direct integration of an interface is good, and the parcelable way is more complex, not only need to integrate parcelable interface also need to rewrite the method inside.

An instance of an object implementing serialization:

Serialization by implementing the Serializable interface:

With so many conceptual knowledge, let's take a look at how to serialize in these two ways, let's first look at how to serialize through the implementation of the serializable interface, and realize the serialization through the implementation of the Serializable interface, Simply implement the Serialiizable interface. By implementing the Serializable interface, the tag type is serialized, and no other action is required.

/** * Created by Aaron on 16/6/29. * * Public  class  person implements Serializable{    Private intAgePrivateString name;PrivateString address; Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } PublicStringgetaddress() {returnAddress } Public void setaddress(String address) { This. address = Address; }}

We can see that we have defined a normal entity person class, and set three member properties and their respective set,get methods, and then we simply implemented the serializable interface as the equivalent of serializing the class. There is no problem when we transfer objects of that type in the program.

Serialization by implementing the Parcelable interface:

And then we're looking at a way to implement the Parcelable interface for serialization by implementing the Parcelable interface, which is a little more complicated to implement the Serialiable interface, because it needs to implement some specific methods, Let's take a look at how the Parcelable interface is implemented, as an example of the person class we define:

/** * Created by Aaron on 16/6/29. * * Public  class  person implements parcelable{    Private intAgePrivateString name;PrivateString address; Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } PublicStringgetaddress() {returnAddress } Public void setaddress(String address) { This. address = Address; }@Override     Public int describecontents() {return 0; }@Override     Public void Writetoparcel(Parcel dest,intFlags) {Dest.writeint ( This. age); Dest.writestring ( This. name); Dest.writestring ( This. address); } Public  Person() {    }protected  Person(Parcel in) { This. Age = In.readint (); This. Name = In.readstring (); This. Address = in.readstring (); } Public Static FinalCreator<person> Creator =NewCreator<person> () {@Override         PublicPersonCreatefromparcel(Parcel Source) {return Newperson (source); }@Override         PublicPerson[]NewArray(intSize) {return NewPerson[size]; }    };}

It can be found that when we implement serialization by implementing the Parcelable interface, we also need to rewrite the member methods inside, and these member methods are fairly fixed.

Implementing Parcelable serialization for Android Studio plugins:

By the way, recently found a relatively good parcelable serialization plugin. Here's how to install and use the plugin.

    • Open Android studio–> settings–> plugins–> search parcelable–> install–> Restart so that the parcelable plugin is installed;

    • Then right-click –> generate...–> in the source file parcelable

    • After clicking Parcelable, you can see that the Parcelable interface has been implemented in the source file and the corresponding method has been rewritten:

This allows us to install the parcelable plugin and then rewrite the corresponding serialization method of the Parcelable interface when we perform the parcelable operation.

Summarize:

    • Serialization can be implemented by implementing the serializable and Parcelable interfaces

    • Implementing the Serializable interface is a way to implement serialization in Java, and implementing parcelable is a unique way to implement serialization in Android, which is more suitable for Android environments

    • Implementing the Serializable interface requires only implementing the interface without additional action, and implementing the Parcelable interface needs to override the appropriate method

    • Android Studio has the corresponding plug-in implementation of the Parcelable interface, can be installed the plug-in is easy to implement the Parcelable interface, the implementation of serialization


In addition to product development technology, skills, practice interested students can refer to my:
Android Product Development (i) –> Practical development Code
Android product Development (ii) –> start Page optimization
Android Product Development (iii) –> base class activity
Android product Development (iv) –> reduce APK size
Android Product Development (v) –> multi-channel packaging
Android product Development (vi) –>APK confusion
Android Product Development (vii) –>APK hot fix
Android product Development (eight) –>app data statistics
Android product Development (ix) –>APP Network Transfer Protocol
Android Product Development (10) –> do not use static variables to save data
Android Product Development (11) –> in-app jump scheme protocol
Android Product Development (12) –>app Long Connection implementation
Android Product Development (13) –>app rotation operation
Android Product Development (14) –>app Upgrade and update

This article synchronizes to GitHub: Https://github.com/yipianfengye/androidProject, Welcome to star and follow

Android Product Development (15)--Memory object serialization

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.