Android USES Intent to pass data implementation ideas and code

  • 2020-05-07 20:28:44
  • OfStack

Intent Android is a very important concept, with the intention of the word (intention, purpose) 1 sample, the role of the class in Android is to invoke a form to do a 1 thing, such as through startActivity start a Activity, through startService start a Service, sent via sendBroadcast 1 radio, etc., it is equivalent to a bridge between each form. The important point is that the process can be cross-process, such as in the application can start to call a player component (the system itself or the third party) to play a video, start the camera to take a picture, and so on.

When we start the video player to play a video, we first need to tell the player which video file to play. This involves the data transfer function of intent. Intent can transfer data in two ways: setData and putExtra. The former parameter is Uri, Uri is a global identifier of accessible data, such as a file on disk, a contact in the database, etc., while the latter parameter can be a specific basic data type and object. For an object, the members of the object are passed, including the basic data members and member objects. Intent requires that the passed object and its member objects implement Serializable or Parcelable, and that they can be persisted (it is understandable that the object might be saved to a buffer region before being passed, and then retrieved from that region).

When first came into contact with android, I was confused by one of them. Let's look at the code first:
 
// 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 ActivityB, mValue of cls has been changed, why mMyCls and mValue have not changed in ActivityA? Is cls in B a copy of mMyCls in A? Why is API not covered? There was a long struggle to figure out whether intent was passing an object reference or a copy of an object. But now can be relieved, believed to have the same doubts after reading this article readers will be relieved.

For complex objects, let's take a tree node as an example. If a tree node has a queue of its member objects with parent node and child node, when such a node is passed by Intent, it will continue to recurse, resulting in the fact that the whole tree is passed! If the tree is bigger, the efficiency is 10 cents lower. The transient modifier solves this problem. 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 queue of parent and child nodes in the node to transient, so that they are not passed in the process of transmission. When received, the parent and child nodes are null. But it's clear that this can lead to lost information.

1 what happens when you don't need to access other components across processes, and the complex objects you pass need to ensure that complete information is not lost? Quite simply, since memory is accessible to everyone in the same 1 process, passing this object with intent is a kill, not intent. For example, you can store the tree node in a global variable and the destination component can access the global variable directly.

Related articles: