Overview
Intent is an important way for various components in an Android program to interact. It not only indicates the actions the current component wants to perform, but also passes 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 isIntent(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 you want to start
This constructor can be used to construct an Intent, and the Activity class provides astartActivity()
Method, specifically starts the Activity, which receives an Intent parameter. We define a button button1 to modify the click event
{ val intent = Intent(this, SecondActivity::) startActivityForResult(intent) }
In this way, the intent of starting an activity is very obvious, so it is called an explicit Intent
2. Implicit Intent
The implicit Intent does not specify which activity to start, but instead specifies a series of more abstract actions and categories and other information, and then hand it over to the system to analyze the Intent and help us find the appropriate activity to start.
By configuring the content of <intent-filter> in the <activity> tag, you can specify the action and category that the current Activity can respond to, open it, and add the following code:
<activity android:name=".SecondActivity"> <intent-filter> <action android:name=".ACTION_START" /> <category android:name="" /> <category android:name=".MY_CATEGORY" /> </intent-filter> </activity>
We indicate that the current Activity can respond.ACTION_START
This action, and the <category> tag contains some additional information. This activity can only respond to the Intent if the contents in <action> and <category> match the action and category specified in the Intent at the same time.
{ val intent = Intent(".ACTION_START") (".MY_CATEGORY") startActivity(intent) }
Using an implicit Intent can not only start activities within your own program, but also activities in other programs, which makes sharing of functions between multiple applications possible. For example, your application needs to display a web page, and you only need to call the system's browser to open the web page.
{ val intent = Intent(Intent.ACTION_VIEW) = ("") startActivity(intent) }
Use Intent to pass data
1. Pass data to the next activity
The idea of passing data when starting an Activity is very simple. Intent provides a series of overloads of putExtra() methods. You can temporarily store data in the Intent. When starting another Activity, you can then take the data out of the Intent.
{ val data = "Hello SecondActivity" val intent = Intent(this, SecondActivity::) ("extra_data", data) startActivity(intent) }
Then fetch the passed data in SecondActivity
override fun onCreate(savedInstanceState: Bundle?) { (savedInstanceState) setContentView(.sceond_layout) val extraData = ("extra_data") ("SecondActivity", "extra data is $extraData") }
2. Return data to the previous Activity
There is a startActivityForResult() method in the Activity class that starts the Activity, which can return the result 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 the data in the subsequent callback.
Modify the click event code of the button in FirstActivity as follows. Here is the startActivityForResult() method to start SecondActivity. As long as the request code is a unique value, 1 is passed here.
{ val intent = Intent(this, SecondActivity::) startActivityForResult(intent, 1) }
Next, register the click event for the button in SecondActivity and add the logic to return data in the click event.
class SecondActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { (savedInstanceState) setContentView(.sceond_layout) { val intent = Intent() ("data_return", "Hello FirstActivity") setResult(RESULT_OK, intent) finish() } } }
An Intent is still built here, but this Intent is only used to pass data, and then the setResult() method is called, which specifically returns data to the previous Activity
The setResult() method receives two parameters:
- The first parameter is used to return the processing result to the previous Activity. Generally, only the two values RESULT_OK or RESULT_CANCELED are used.
- The second parameter passes the Intent with the data back
Since we use the startActivityForResult() method to start SecondActivity, the onActivityResult() method of the previous Activity will be called back after SecondActivity is destroyed, so we need to override the method in FirstActivity to get the returned data
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { (requestCode, resultCode, data) when (requestCode) { 1 -> if (resultCode == RESULT_OK) { val returnedData = data?.getStringExtra("data_return") ("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, which is the processing result we pass in when returning data.
- The third parameter data, that is, the Intent carrying the returned data
Since it is possible to call startActivityForResult() in an Activity to start many different activities, and the data returned by each Activity will be called back to the onActivityResult() method, the first thing we need to do is to check the value of requestCode to determine the data source, and then judge whether the processing result is successful by using the value of resultCode. Finally, take the value from data and print it
This is the article about the introduction to Android: Intent that shuttles between activities. For more related Android Intent content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!