【Android Intent】

There are two main functions of Intent in Activity:

1. Start the target Activity

2. Pass data

Intent is divided into two situations when passing data: passing data to the next Activity and returning data from the next Activity.



In Android, objects are passed between Activity and Fragment. You can serialize the objects and store them in a Bundle or Intent for transmission, or you can convert the objects into JSON strings for transmission. Serialized objects can use Java's Serializable interface and Parcelable interface. Convert it into a JSON string, you can use libraries such as Gson.


Although the Serializable method is very simple, the efficiency is not optimistic, because it will serialize the entire object, and the overhead is very large. In order to pursue efficiency, we have to use another method, which is the Parcelable method.



The custom class implements the Parcelable interface. At this time, two methods must be rewritten

1.describeContents(): used to describe the content interface, generally return 0 directly

2.writeToParcel() : used to write the data you want to pass into the Parcel container.

In addition to these two methods, we also need to create an implementation of the Parcelable.Creator interface, which also requires us to implement two methods

1.createFromParcel(): used to read the data written in the Parcel container, instantiate an object with the read data, and return.

2. newArray() : Create an array of length size and return it. Generally speaking, return T[size] directly.


Guess you like