Andoid Intent learns to transfer data between various activities, andoidintent
Intent is a run-time binding mechanism, which can be used to connect two different components while the program is running. With Intent, your program can express a request or willingness to Android. Android selects appropriate components based on the content you want to complete the request.
1. Use Intent to transmit data to the next activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btn1" /></LinearLayout>
2. Write a listening event for Button1 in MainActivity
(Button) findViewById (R. id. btn1 ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {/* use Intent to transmit data to the next activity */String data = "Hello World !!! "; Intent intent = new Intent (); intent. setClass (MainActivity. this, SecondActivity. class); intent. putExtra ("extra_data", data); startActivity (intent );});
3. Create a New SecondActivity to receive data from MainActivity
@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_second);/* use Intent to transmit data to the next activity */Intent intent = getIntent (); String data = intent. getStringExtra ("extra_data"); Toast. makeText (SecondActivity. this, data, Toast. LENGTH_SHORT ). show ();}
2. Use Intent to return data to the previous activity
1. Add a Button2 in SecondActivity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.tony.intenttest.SecondActivity"> <Button android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btn2" /></LinearLayout>
2. Write a listening event for Button1 in MainActivity
Public class MainActivity extends Activity {private Button btn1; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_main); btn1 = (Button) findViewById (R. id. btn1); btn1.setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {/* use Intent to return data to the previous activity */Intent intent = new Intent (MainActivity. this, SecondActivity. class); startActivityForResult (intent, 1 );}});}
Note: The startActivityForResult () method receives another parameter. The first parameter is Intent, and the second parameter is the Request Code (the request code can be a unique value ), it is used to determine the data source in the subsequent callback.
3. Register a click event for Button2 in SecondActivity
Public class SecondActivity extends Activity {private Button btn2; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_second);/* use Intent to return data to the previous activity */btn2 = (Button) findViewById (R. id. btn2); btn2.setOnClickListener (new View. onClickListener () {@ Override public void onCl Ick (View v) {Intent intent = new Intent (); intent. putExtra ("return_data", "Hello MainActivity !!! "); SetResult (RESULT_ OK, intent); finish () ;}});}/* onBackPressed () If you press the return key (Back key) call this method */@ Override public void onBackPressed () {Intent intent = new Intent (); intent. putExtra ("return_data", "Hello MainActivity !!! "); SetResult (RESULT_ OK, intent); finish ();}}
Note: The putExtra () method receives two parameters. The first parameter is the key, which is used to return values from Intent. The second parameter is the data to be transmitted.
An Intent is built here, but this Intent is only used to transmit data. It does not specify any "Intent ". Next, place the data to be passed in the Intent and call the setResult () method.
The setResult () method receives two parameters. The first parameter is used to return the processing result to an activity. Generally, only RESULT_ OK and RESULT_CANCLED are used. The second parameter is used to pass the Intent with data back, then call the finish () method to destroy the current activity.
4. receive data in MainActivity and rewrite the onActivityResult () method.
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case 1: if (resultCode == RESULT_OK) { String returnedData = data.getStringExtra("return_data"); Log.d("tag", returnedData); } break; default: } }
Note: The startActivityForResult () method is used to start SecondActivity. After SecondActivity is destroyed, the onActivityResult () method of the previous activity is called back. Therefore, in MainActivity
To override the onActivityResult () method.
The onActivityResult () method has three parameters: the first parameter requestCode, that is, the input request code during the startup activity, and the second parameter resultCode, that is, the processing result we pass in when returning data. The third parameter data is the Intent of the data returned by the carrier.
Because the startActivityForResult () method may be called in an activity to start many different activities, the data returned by each activity will be called back to the onActivityResult () method. Therefore, you must pass the check
The value of requestCode is used to determine the data source. After the data is returned from SecondActivity, The resultCode value is used to determine whether the processing result is successful.