Intent passes the difference between Serializable and Parcelable for an object

  • 2021-01-06 00:43:14
  • OfStack

The use of Intent to pass object data between different components is very common. It is well known that there are two ways to pass objects in intent: 1) implement the Serializable interface; 2) implement the Parcelable interface.

In Android Intent Serializable, two methods of transfer object Parcelable please click for details.

Why serialize objects?

1. Perpetually save the object and save the byte sequence of the object to the local file;
2. Use serialized objects to transfer objects in the network;
Passing objects from process to process by serializing them.

1. Realize the Serializable interface

The function of Serializable is to store the data object into the byte stream, and regenerate the object when needed. The main applications are to save the state of the object by using external storage device, and transfer the object through the network.

What the implements interface does is to tag an object and the system will automatically serialize it.

Case 1:

1) (implements Serializable )


User user = new User();
Intent intent = new Intent(this,Second.class); 


Intent intent = getIntent();
User user = intent.getSerializableExtra("user");

2. Implement Parcelable interface

1) Why implement the Parfcelable interface to implement passing objects in Intent?
When using memory, Parcelable performs better than Serializable, so it is recommended to use the Parcelable class.
b, Serializable are serialized to produce a large number of temporary variables, resulting in frequent GC.

Note: Parcelable cannot be used in the case of storing data on disk, because Parcelable does not preserve data persistence well in the case of external changes. Therefore, Serializable is recommended in this case

2) New serialization mechanism in ES69en

In the Android system, for the memory limited mobile devices, so the performance requirements are higher, Android system adopts the new IPC(inter-process communication) mechanism, requiring the use of better performance of object transfer mode. Therefore, the ES75en class was designed to be positioned as a lightweight and efficient object serialization and deserialization mechanism.
Parcel serialization and deserialization reads and writes are all in memory, so the efficiency is much higher than the use of external memory in JAVA serialization.

Parcel class

As far as the application is concerned, the scenario where the ES85en class is often used is to pass data between ES86en. When using Intent to pass data between Activity, complex objects can be passed through the Parcelable mechanism.

Parcel mechanism: Think of it as an Serialize, essentially. It's just that ES95en objects are serialized and deserialized in memory, using contiguous memory space, so it's more efficient.

Case study:

Step 1: Customize the entity class, implement the Parcelable interface, and override its two methods.
Step 2: The entity class must add a constant CREATOR (name cannot be capitalized otherwise), which must implement Parcelable's internal interface: Parcelable.Creator, and implement two methods in that interface.

User. java as follows:

package com.example.intent_object; 
import android.os.Parcel; 
import android.os.Parcelable; 
public class User implements Parcelable { 
public String name; 
public int age; 
//  You have to create 1 A named CREATOR The constants.  
public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() { 
public User createFromParcel(Parcel source) { 
return new User(source); 
// rewrite createFromParcel Method, create and return 1 I got the data user object  
public User[] newArray(int size) { 
return new User[size]; 
public String toString() { 
return name + ":" + age; 
//  A no-parameter constructor method that is called when the outside world creates an instance of the class  
public User() { 
//  The constructor method with arguments is private. This constructor is available only for methods of a class createFromParcel call  
private User(Parcel source) { 
name = source.readString(); 
age = source.readInt(); 
public int describeContents() { 
return 0; 
//  Saves attributes from the object to the target object dest In the  
public void writeToParcel(Parcel dest, int flags) { 
// omit getter/setter } 

Other codes:

Bundle bundle = new Bundle(); 
bundle.putParcelable("user", user); 
Intent intent = new Intent(MainActivity.this, 
Intent intent = getIntent(); 
Bundle bun = intent.getExtras(); 
User user = bun.getParcelable("user"); 

The above content is this article to introduce the Intent Serializable and Parcelable difference of transfer object, the following to everyone in the explanation Android Intent Serializable, two methods of transfer object Parcelable, interested friends can click for details.

Related articles: