summary
Intent is an important way of interaction between components in Android programs. It can not only indicate the actions that the current component wants to perform, but also transfer data between different components
Intent can be roughly divided into two types: explicit intent and implicit intent
1. Explicit Intent
Intent has multiple constructor overloads, one of which is intent (context, packetcontext, class <? > CLS). This constructor receives two parameters:
- The first parameter, Context, requires a Context to start the Activity
- The second parameter Class is used to specify the target Activity to start
This constructor can be used to build Intent. The Activity class provides a startActivity() method to start the Activity, which receives an Intent parameter. We define a button button1 to modify the click event
button1.setOnClickListener { val intent = Intent(this, SecondActivity::class.java) startActivityForResult(intent) }
When you start an Activity in this way, Intent's intention is obvious, so it is called explicit Intent
2. Implicit Intent
The implicit Intent does not specify which Activity you want to start, but specifies a series of more abstract information such as action and category, which is then handed over to the system to analyze the Intent and help us find a suitable Activity to start
By configuring the content of < intent Filter > in the < Activity > tab, you can specify the action and category that the current Activity can respond to, open AndroidManifest.xml, and add the following code:
<activity android:name=".SecondActivity"> <intent-filter> <action android:name="com.example.activitytest.ACTION_START" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="com.example.activityest.MY_CATEGORY" /> </intent-filter> </activity>
We indicated that the current Activity can respond to com.example.activitytest.ACTION_START is the action, and the < category > tag contains some additional information. This Activity can respond to Intent only if the contents in < action > and < category > match the action and category specified in Intent at the same time
button1.setOnClickListener { val intent = Intent("com.example.activitytest.ACTION_START") intent.addCategory("com.example.activityest.MY_CATEGORY") startActivity(intent) }
Using implicit Intent, you can start not only the activities in your own program, but also the activities of other programs, which makes it possible to share functions among multiple applications. For example, if your application needs to display a web page, you just need to call the system browser to open the web page
button1.setOnClickListener { val intent = Intent(Intent.ACTION_VIEW) intent.data = Uri.parse("https://www.baidu.com") startActivity(intent) }
Passing data using Intent
1. Pass data to the next Activity
The idea of transferring data when starting an Activity is very simple. Intent provides a series of overloads of putExtra() methods. You can temporarily store the data in intent. When starting another Activity, you can take the data out of intent
button1.setOnClickListener { val data = "Hello SecondActivity" val intent = Intent(this, SecondActivity::class.java) intent.putExtra("extra_data", data) startActivity(intent) }
Then take out the passed data in SecondActivity
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.sceond_layout) val extraData = intent.getStringExtra("extra_data") Log.d("SecondActivity", "extra data is $extraData") }
2. Return data to the previous Activity
In the Activity class, there is a startActivityForResult() method to start an Activity, which can return results to the previous Activity when the Activity is destroyed. This method receives two parameters:
- The first parameter is Intent
- The second parameter is the request code, which is used to determine the source of data in the subsequent callback
Modify the click event code of the button in FirstActivity as follows. The startActivityForResult() method is used to start SecondActivity. As long as the request code is a unique value, 1 is passed in here
button1.setOnClickListener { val intent = Intent(this, SecondActivity::class.java) startActivityForResult(intent, 1) }
Next, register the click event for the button in the SecondActivity, and add the logic of returning data to the click event
class SecondActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.sceond_layout) button2.setOnClickListener { val intent = Intent() intent.putExtra("data_return", "Hello FirstActivity") setResult(RESULT_OK, intent) finish() } } }
Here is still building a Intent, but this Intent is only used to pass data, and then invoked the setResult() method, which specifically returns data to the last Activity.
The setResult() method receives two parameters:
- The first parameter is used to return the processing result to the previous Activity. Generally, only result is used_ OK or RESULT_CANCELED these two values
- The second parameter passes back the Intent with data
Since we use the startActivityForResult() method to start the SecondActivity, the onActivityResult() method of the previous Activity will be called back after the SecondActivity is destroyed. Therefore, we need to rewrite this method in FirstActivity to get the returned data
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when (requestCode) { 1 -> if (resultCode == RESULT_OK) { val returnedData = data?.getStringExtra("data_return") Log.d("FirstActivity", "returned data is $returnedData") } } }
The onActivityResult() method takes three parameters:
- The first parameter, requestCode, is the request code passed in when we start the Activity
- The second parameter, resultCode, is the processing result passed in when we return data
- The third parameter, data, is the Intent that carries the returned data
Because startActivityForResult() may be called in an Activity to start many different activities, and the data returned by each Activity will be called back to onActivityResult() method, the first thing we need to do is to judge the data source by checking the value of requestCode, and then judge whether the processing result is successful by the value of resultCode, Finally, take values from data and print them