Intent
Intent is an important way for each component in an Android program to interact. It not only specifies the actions the current component wants to perform, but also specifies the activities you want to initiate, but also passes data between different components.
Part 1: Start an activity through intent
Intent is divided into display and implicit
Let's first understand the display
One of the commonly used constructors is:
Intent intent=new Intent(,); //In this way we construct an Intent
The first parameter is Context, requiring an activity context (telling who you want to execute this operation)
The second parameter is class, which indicates the target you want to start (indicate which program you want to execute). Here is a reference
Through this construction method, you have constructed the Intent
Next, start using this Intent
Start the target through the startActivity() method Here is the reference
Implicit
When the intent target, i.e. the target component name cannot be determined, is the target component name, start using an implicit Intent. Components commonly used to start other applications.
For example: Implicit Intent starts the system's default browser:
//Prepare the data attribute data of the IntentUri uri=(""); //Set the action attribute and data attribute of the IntentIntent intent=new Intent(Intent.ACTION_VIEW,uri); //Start the goal intentionstartActivity(intent);
Part 2: Passing data to the next activity through Intent
Intent provides a series of overloading of putExtra() methods
It is equivalent to temporarily exposing the data we want to pass in the intent first. After we start another activity, we just need to take this data out of the intent.
private void passdate() { //Create an intent object Intent intent=new Intent(,); //Save data into intent ("name",et_name.getText().toString().trim()); ("password",et_password.getText().toString().trim()); //Open intention startActivity(intent); }
Note: ShowActivity is started by displaying the intention, and a string is passed through the putExtra() method. The putExtra() method receives two parameters. The first parameter is the key: used for the value in the subsequent intent, and the second parameter is the data we really want to pass.
//Get the intent object to open the page Intent intent=getIntent(); //Fetch the corresponding value according to the key String name=("name"); String password=("password"); //Show the username and password on the control (overlay the control location) TextView tv_name=(TextView) findViewById(.tv_name); TextView tv_password=(TextView) findViewById(.tv_psw); tv_name.setText(name); tv_password.setText(password);
- First, get the intent object of the previous activity, and use the getIntent() method to obtain the intent that starts the showActivity.
- Then call the getStringExtra method and pass the corresponding key value to get the passed data
- If you want to pass plastic surgery, getIntExtra() method gets the data, and so on
- Finally, start execution through setText() and display our data in the control position.
Part 3: Return data to the previous activity
We usually use the startActivityForResult() method, which is also used to start the activity, but it can return a result to the previous activity after the activity is destroyed.
protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); final EditText editText1 = (EditText) findViewById(.et_name); Button button1 = (Button) findViewById(); (new () { @Override public void onClick(View v) { Intent intent = new Intent(, ); Bundle bundle = new Bundle(); ("name", (CharSequence) ().toString()); //Save data through bundle (bundle); startActivityForResult(intent, 1);//It's very similar to startActivity, so there is an additional request code for later judging the data source } });
The startActivity() method receives two parameters. The first parameter is the Intent and the second parameter is the request code. It is used to determine the data source afterwards.
@Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_show); final EditText editText2= (EditText) findViewById(.show_name); Button button2=(Button) findViewById(.show_button); //Get the intent and bundle of the previous activity Intent intent=getIntent(); Bundle bundle=(); (new () { @Override public void onClick(View v) { Intent intent=new Intent(,); Bundle bundle=new Bundle(); ("edit",(CharSequence) ().toString()); (bundle); //setResult method is used to pass data to the previous activity. The first parameter returns the result of the previous activity. The second parameter is to pass the intent with data over the next. setResult(1,intent); finish(); } }); }
In the showActivity activity, an Intent is also built, which is used to pass data.
By calling the setResult() method, the setResult() method receives two parameters. The first parameter is used to return the processing result to the previous activity. Generally, two values of RESULT_OK or RESULT_CANCELED are used. The second parameter passes the Intent with the data back, and finally destroys the current activity through finish()
//After the finish in the showActivity is destroyed, an active onActivityResult() method will be called back. Therefore, we need to rewrite this method in MainActivity@Override //RequestCode is the request code passed in when we start. The resultCode is passed in when returning data. The data carries the intent of the return data. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { (requestCode, resultCode, data); switch (requestCode) { case 1: if (resultCode == RESULT_FIRST_USER) {//Judge the source of the data through the request code et_name = (EditText) findViewById(.et_name); et_name.setText(("edit"));//Take the value from data and output it //Get the passed data through getStringExtra } break; default: } }
Since we use the startActivity() method to start SecondActivity, after SecondActivity is destroyed, the onActivityResult() method of the previous activity will be called back, so we need to rewrite this method in the previous activity to get the returned data.
The OnActivityResult() method has three parameters, = the first parameter requestCode, the request code to start the activity, the second parameter resultCode, returns the processing result of the data, and the third parameter, the intent that returns the data. Since an activity may call startActivityForResult() to start many different activities, each activity will return the data callback onActivityResult() method, so for safety, Switch statements are generally used to make judgments.
Determine the data source through resquestCode, determine whether the processing result is successful, and finally obtain the value in data and print it out
Relationship between Intent and Bundle in android
For example: China ships the fruits packed from ship to the United States
China is Activity1, the United States is Activity2, shipping is Intent, and the box containing fruit is bundle. The number on the box is key, and the fruit is data.
China specifies the intention of shipping to the United States through new intent object, and puts the boxed fruit on the way to shipping through putExtras.
The United States first needs to obtain the shipping object through getIntent, then obtain the box through getExtras, and then obtain the fruit according to the number on the box.
If there is only one data passed by Intent, then use the putExtra() method of Intent to directly put the parameters into it. If the Intent needs to pass several parameters or a class, then you need to use a Bundle
This is the end of this article about the detailed explanation of the use of Intent and Bundle in Android. For more related content on Android Intent and Bundle, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!