The implementation idea and code _android of Android using intent transfer data

Source: Internet
Author: User
Intent is a very important concept in Android, like the meaning (intent, purpose) of the word, which is used in Android to invoke a build to do something, such as starting an activity through startactivity, Starting a service through the StartService, sending a broadcast through the Sendbroadcast, is equivalent to a bridge between the various builds. It is very important that this process can be across processes, such as in the application can launch a call to a player component (the system itself or a third party) to play a video, start the camera program to take photos and so on.

When we start the video player to play a video, first tell the player which video file to play, this involves intent data transfer function, intent transfer data in two ways: Setdata,putextra. The former argument is that Uri,uri is a global identifier for the data that can be accessed, such as a file on a disk, a contact in a database, and so on, whose parameters can be specific basic data types and objects. For an object, a member of the object is passed, including basic data members and member objects. Intent requires that the objects passed and their member objects implement Serializable or parcelable, which can be persisted (taking into account the passing of the step, it is possible to save the object to a buffer field before passing, and then take it from the area).

when I first contacted Android, there was a confusion, first look at the code
Copy Code code as follows:

Mycls
Class Mycls implements Seriliazable
{
Public String Mvalue;
}


Activitya
Mycls Mmycls;
Jump to Activityb
Intent Intent = new Intent ();
Mmycls = new Mycls ();
Mmycls.mvalue = "Activitya";
Intent.putserializable ("Mycls", mmycls);
Intent.setclass (this, activityb.class);
StartActivity (Intent);

Activityb
Mycls cls = (mycls) getintent (). Getserializable ("Mycls");
Cls.mvalue = "Activityb";

My question is: In the Activityb, the CLS Mvalue changed, why Activitya, mmycls.mvalue unchanged? Is the CLS in B a copy of Mmycls in A, and why is the API not mentioned? The long time tangled in the intent pass is the object reference or the object copy without solution, on the Internet, there is no answer to the information. And now can be relieved, I believe the same confused readers read this article will be relieved.

For complex objects, now example of the tree node, a tree node, whose member object has a parent node and child nodes of the queue, in the intent pass such a node, will continue to recursion, resulting in the actual transmission is the whole tree! If the tree is more inflated, the efficiency will be very low. The transient modifier solves this problem, and if you declare an instance variable with transient, its value does not need to be maintained when the object is stored. We can modify the parent node and child node queues in the node to transient so that they are not passed during the pass, and the parent and child nodes are null when they are received. But clearly this can result in lost information.

In general, there is a situation where you do not have to access other components across processes, and the complex objects you pass need to ensure that the complete information is not lost. Very simple, since in the same process, memory for everyone is accessible, with intent pass this object is really a sledgehammer kill chicken, do not intent pass it. For example, the tree node can be stored in the global variable, the purpose component directly access the global variable can be.
Related Article

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.