SoFunction
Updated on 2025-03-11

Implementation ideas and code for using Intent to pass data on Android

Intent is a very important concept in Android. Like the original meaning (intention, purpose) of this word, the role of this class in Android is to call a certain organization to do something, such as starting Activity, starting a Service through startService, sending a broadcast through sendBroadcast, etc. It is equivalent to a bridge between each organization. It is very important that this process can be cross-process, such as in the application, you can start calling a certain player component (the system itself or a third-party) to play a video, start the camera program to take pictures, etc.

When we start the video player to play a video, we must first tell the player which video file to play. This involves the data transfer function of the intent. There are two ways to transfer data in Intent: setData and putExtra. The former parameter is Uri, which is an identifier of globally accessible data, such as a file on disk, a contact in the database, etc. The latter parameter can be a specific basic data type and object. For objects, the members of the object are passed, including basic data members and member objects. The object passed and its member objects are Serializable or Parcelable, which can be persisted (considering the name delivery, it can be understood that the object may be saved to a buffer area before passing, and then taken from this area).

When I first came into contact with Android, I had a confusion. Let's look at the code first
Copy the codeThe code is as follows:

// MyCls
class MyCls implements Seriliazable
{
public String mValue;
}


// ActivityA
MyCls mMyCls;
// Jump to ActivityB
Intent intent = new Intent();
mMyCls = new MyCls();
= "ActivityA";
("MyCls", mMyCls);
(this, );
startActivity(intent);

// ActivityB
MyCls cls = (MyCls)getIntent().getSerializable("MyCls");
= "ActivityB";

My doubt is: In ActivityB, the mValue of cls has been changed, why hasn't it changed in ActivityA? Is cls in B a copy of mMyCls in A? Why didn't the API mention it? I had been struggling with whether the object reference or copy of the intent was passed for a long time and could not solve it. I had no answers to searching for information online. And now I can feel relieved. I believe readers who have the same doubts will feel relieved after reading this article.

For complex objects, we will now give an example to illustrate the tree node. A tree node has a queue of parent nodes and child nodes. When passing such a node with Intent, it will continue to recurse, resulting in the entire tree actually being passed! If this tree is enlarged, its efficiency will be very low. The transient modifier can solve this problem. If an instance variable is declared with transient, its value does not need to be maintained when the object is stored. We can modify the parent node and child node queue into transient in the node, so that they will not be passed during the delivery process. During reception, the parent node and child node are null. But it is obvious that this will lead to the loss of information.

Generally speaking, there will be such a situation: there is no need to access other components across processes, and the complex objects passed need to ensure that complete information is not lost. What should I do? It's very simple. Since memory is accessible to everyone in the same process, passing this object with intent is really a big deal, and you don't need to pass it intent. For example, you can store this tree node in a global variable, and the destination component can directly access the global variable.