Android notes Intent usage Summary
Intent:
Official explanation:
An intent is an abstract description of an operation to be performed med. It can be usedstartActivityTo launchActivity,broadcastIntentTo send it to any interestedBroadcastReceiverComponents, andstartService(Intent)OrbindService(Intent, ServiceConnection, int)To communicate with a backgroundService.
An intent is an abstract description to execute an operation. It can be used for startActivity to launch an activity. The broadcastIntent BroadcastReceiver component sends it to any interested component and communicates with the background service by startService (intent) or bindService (ServiceConnection intent, int.
An Intent provides a facility for semantic Ming late runtime binding between the code in different applications. its most significant use is in the launching of activities, where it can be thought of as the glue between activities. it is basically a passive data structure holding an abstract description of an action to be performed med.
An intent provides a tool for executing code to bind delayed runtime between different applications. The most important purpose is the launch of the activity, which can be considered as the glue between the activities. It is basically an action of passively holding an abstract data structure.
Explain the functions and usage of Intent by yourself:
Intent is one of the important ways to interact with each component in the Android program. It can specify the action to be completed in the current Activity and transmit data between different components. Intent can be divided into two types: Intent and implicit Intent.
1. Display Intent
First, we will introduce how to display Intent. One of the Intent constructor Intent (Context pageContext, Class Cls) the following constructor instantiates a display Intent. This constructor receives two parameters. The first parameter Context needs to fill in the Context of the startup Activity, which is generally the current Activity, the second parameter Class is to specify the target activity to be started. Through this constructor, we can construct an Intent object that represents our "Intent.
Modify the Click Event of the button in FirstActivity. The Code is as follows:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + ICDU2snPw + bA/queues + 62r6Os1eLR + c7Sw8e + queues/queues + queues/ydLUxvS2r8S/queues + queues/ gscjS/examples/ExLj2u + examples/gudjQxc + examples/Ipcb0tq + hozwvcD4KPHA + samples/samples + 38 zltcS/samples + cve-vcd4kpha + samples/samples + samples /pushed + 3S48YnI + pushed + tPryv2 + 3bf + zvHG98nPtcTK/pushed + rv6vq + pushed/S48L3A + pushed/pushed = "http://www.2cto.com/uploadfile/Collfiles/20141206/2014120608534296.png "alt =" \ ">
In the above example, we first Initialize an Intent object and specify the action parameter. Next, call addCategory () in Intent to add a category. Only one action can be specified for each Intent, but multiple category can be specified.
To enable SecondActivity to continue responding to this Intent, we can configure it under the tag in the AndroidManifest. xml file. To specify the actions and category that the current activity can respond.
3. transmit data to the next activity through Intent
It is easy to pass data when an Intent object starts an activity. Execute the PutExtra () method called in the current activity to save the data to be passed in the Intent, and extract the data in Intent from the newly started Activity. For example, if we can pass a string in FristActivity to SecondActivity, we can implement it as follows:
In the above example, we start SecondActivity by displaying Intent, and pass a string to SecondActivity through the putExtra () method. PutExtra () method
Receives two parameters. The first parameter is the key and the second parameter is the value, which indicates the data to be passed.
Next we will extract the data from the Intent object in SecondActivity and perform other operations. The Code is as follows:
In the above Code, call the getInstent () method to obtain the Intent used to start SecondActivit, call the getStringExtra () method of the Intent object, and input the corresponding key name, you can get the data that comes in.
4. return data to the previous activity through the Instent object
The content above describes how to pass data to the next activity through the Intent object. Here we will discuss how to return data to the previous activity. To return data to the previous activity, we can call the startActivityForResult () method, which is also used to start the activity, however, this method is expected to return a result to the previous activity when the activity is destroyed. The startActivityForResult () method receives two parameters: the first parameter is Intent, and the second parameter is the request code used to determine the source of the callback. Must the request be unique. The usage is as follows:
In this example, startActivityForResult (intent, 1); is used to start SecondActivity. In SecondActivity, register the click event for the button mybutton2 and click
Click the event to add the returned data logic. The Code is as follows:
We first instantiate an Intent object, encapsulate the data to be passed in the object, and then call the setResult () method. This method is used to return data to an activity and receive two parameters.
A parameter is used to return processing results to an activity. The second parameter is used to pass the Intent with data back.
The startActivityForResult () method is used to start SecondActivity. After SecondActivity is destroyed, the onActivityResult () method of the previous activity is called back.
So we need to re-write this method in FirstActivity to get the data, as shown below:
The onActivityResult () method in the preceding example has three parameters. The first requestCode is the request code we pass in when starting the activity to determine the message source. The second parameter, resultCode, is the processing result passed in when the data is returned. It is used to determine whether the processing result is successful. The third parameter represents the Intent carrying the returned data.