Small talk

This started to write to share, want to write things is more and more, many have seen before but more vague want to write (manual doge, not, today saw a project using Parcelable, just want to see these two serialization methods

The introduction of

Intent.putextra () = intent.putextra () = intent.putextra () = intent.putextra () What’s the difference between these two?

Say first use

Serializable

This is the standard Java serialization method, no other advantages, simple and easy to use, here is good, not to say performance, but the use of feeling = =. It’s simple to use, just implement the Serializable interface directly. Super mindless

Parcelable

CREATOR is a non-empty static field that implements the Parcelable. CREATOR class.

public class MyParcelable implements Parcelable {
    private int mData;

    public int describeContents(a) {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(mData);
    }

    public static final Parcelable.Creator<MyParcelable> CREATOR
            = new Parcelable.Creator<MyParcelable>() {
        public MyParcelable createFromParcel(Parcel in) {
            return new MyParcelable(in);
        }

        public MyParcelable[] newArray(int size) {
            return newMyParcelable[size]; }};private MyParcelable(Parcel in) { mData = in.readInt(); }}Copy the code

The difference between

  1. Parcelable can serialize in memory faster than Serializable
  2. Serializable uses reflection during serialization and generates a large number of temporary objects

Does anyone think this is the end of the story? Naive! (Push glasses manually

but

None of this is wrong, but it’s not accurate, because Serializable provides more than just automatic serialization, so like Parcelable, you can specify your own serialization:

 private void writeObject(java.io.ObjectOutputStream out)
 throws IOException;

 private void readObject(java.io.ObjectInputStream in)
     throws IOException, ClassNotFoundException;

 private void readObjectNoData(a)
     throws ObjectStreamException;
Copy the code

Here’s a quote from StackOverflow:

Usual Java serialization on an average Android device (if done right *) is about 3.6 times faster than Parcelable for 2. Proves that Java Serialization (if done right) is fast storage mechanism that gives acceptable results even with relatively large object graphs of 11000 objects with 10 fields each. * The sidenote is that usually everybody who blindly states that “Parcelable is mush faster” compares it to default automatic serialization, which uses much reflection inside. This is unfair comparison, because Parcelable uses manual (and very complicated) procedure of writing data to the stream. What is usually not mentioned is that standard Java Serializable according to the docs can also be done in a manual way, using writeObject() and readObject() methods. For more info see JavaDocs. This is how it should be done for the best performance.

Serializable is fast, 1.6 times faster than Parcelable reads and 3.6 times faster than Parcelable writes on average Android machines. Parcelable is faster than Serializable because Serializable is automatically serialized…

Totally the opposite of what we said before…

In doubt

There’s another story that goes like this

Parcelable cannot be used when you want to store data on disk because Parcelable does not do a good job of keeping the data sustainable in the face of changes in the outside world. Although Serializable has low efficiency and is not recommended, it is recommended that you use Serializable in this case.

The source of this link was written in 11 years, which is already the earliest ONE I have found. I don’t know what the reason is. If you know, you can give advice and exchange

conclusion

I need to borrow the big guy’s code

So, if serializable is faster and easier to implement, why android has parcelable at all? The reason is native code. Parcelable is created not just for interprocess communication. It also can be used for intercode communication. You can send and recieve objects from C++ native layer. That’s it.

What should you choose? Both will work good. But I think that Parcelable is better choice since it is recommended by google and as you can see from this thread is much more appreciated.

So you still need to use these two, at least eliminate automatic serialization, no less performance (manual Doge