In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand the Intent shuttling between Activity". In the daily operation, I believe many people have doubts about how to understand the Intent shuttling between Activity. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to understand the Intent shuttling between Activity"! Next, please follow the editor to study!
Catalogue
Overview
1. Explicit Intent
two。 Implicit Intent
Using Intent to pass data
1. Pass data to the next Activity
two。 Return data to the previous Activity
Overview
Intent is an important way to interact between components in an Android program. 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 several constructors overloaded, one of which is Intent (Context packetContext, Class cls), which takes two arguments:
The first parameter, Context, requires that you provide a context to start Activity.
The second parameter, Class, is used to specify the target Activity you want to start.
Through this constructor, you can build a startActivity () method provided in the Intent,Activity class that specifically launches 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)}
The intention of starting Activity,Intent in this way is very obvious, so it is called explicit Intent
two。 Implicit Intent
The implicit Intent does not specify which Activity to start, but specifies a series of more abstract information such as action and category, and then leaves it to the system to analyze the Intent and help us find a suitable Activity to start.
By configuring the content in the tag, you can specify the action and category that the current Activity can respond to, open AndroidManifest.xml, and add the following code:
We indicate that the current Activity can respond to the action of com.example.activitytest.ACTION_START, while the tag contains some additional information. Only if the action and category specified in the Intent match at the same time, the Activity can respond to the Intent.
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 Activity within your own program, but also the Activity of other programs, which makes it possible to share functions among multiple applications. For example, if your application needs to display a web page, just call the browser of the system to open the web page.
Button1.setOnClickListener {val intent = Intent (Intent.ACTION_VIEW) intent.data = Uri.parse ("https://www.baidu.com") startActivity (intent)} uses Intent to pass data 1. Pass data to the next Activity
The idea of passing data when starting Activity is very simple. Intent provides a series of overloads of putExtra () methods. You can temporarily store the data in Intent, and when you start 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
There is a startActivityForResult () method in the Activity class that starts Activity, which returns the result to the previous Activity when the Activity is destroyed, which takes 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 subsequent callbacks.
Modify the click event code of the button in FirstActivity as follows. Here, the startActivityForResult () method is used to start SecondActivity. As long as the request code is a unique value, pass in 1.
Button1.setOnClickListener {val intent = Intent (this, SecondActivity::class.java) 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?) {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 you build an Intent, except that the Intent is only used to pass data, and then calls the setResult () method, which returns data specifically 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 back the Intent with the data
Since we use the startActivityForResult () method to start SecondActivity,SecondActivity destruction and call back the onActivityResult () method of the previous Activity, we need to override 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 Activity.
The second parameter, resultCode, is the processing result we passed in when we returned the data.
The third parameter, data, is the Intent that carries the returned data
Since it is possible to call startActivityForResult () in an Activity to start many different Activity, and the data returned by each Activity will be called back to the onActivityResult () method, the first thing we need to do is to determine the data source by checking the value of requestCode, and then judge whether the processing result is successful by the value of resultCode. Finally, we take the value from data and print it.
At this point, the study on "how to understand the Intent shuttling between Activity" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.