Basic types of

startActivityForResult(Intent intent, int requestCode)

onActivityResult(int requestCode, int resultCode, Intent data)

setResult(int resultCode, Intent data)

These three functions, pay attention to their arguments! Now there are two activities A and B. If we jump from A to B, and B finishes the corresponding work, we finish B and send the data to A, and A does the corresponding operation after receiving the data. We can use these three functions:

One

ActivityB Intent Intent = new Intent(); intent.setClass(A.this, B.class); startActivityForResult(intent, 2); //2 is our self-defined constant, corresponding to the resultCode used belowCopy the code

Two

//do something
setResult(2, null);
finish();
Copy the code

Override onActivityResult() after the main code above is done:

/ / A:  @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (2 == requestCode) { //do something } }Copy the code

StartActivityForResult and onActivityResult

StartActivityForResult (Intent Intent, int requestCode) is also a common method for jumping between AndroidActivities. The onActivityResult(int requestCode, int resultCode, Intent Data) method is used to retrieve the action after the request Activity ends. There are three approaches to note:

OnActivityResult (int requestCode, int resultCode, Intent data)  setResult(int resultCode, Intent data)Copy the code

For example, jump From From to ToB and ToC

Intent Intent = new Intent(this, tob.class); startActivityForResult(serverIntent, REQUEST_CODE_01); ToB}else{Intent Intent = new Intent(this, toc.class); startActivityForResult(serverIntent, REQUEST_CODE_02); Public void onActivityResult(int requestCode, int resultCode, Intent data){ switch (requestCode) { case REQUEST_CODE_01: if(resultCode==Activity.RESULT_OK) //do something break; case REQUEST_CODE_02: //do something break; }}Copy the code

StartActivityForResult (Intent Intent, int requestCode) We use REQUEST_CODE_01 and REQUEST_CODE_02, which are int constants to mark the Activity that is returned from the onActivityResult method.

ToB: Intent Intent = new Intent(); intent.putExtra(key, value); setResult(Activity.RESULT_OK, intent); finish(); Intent Intent = new Intent(); intent.putExtra(key, value); setResult(Activity.RESULT_OK, intent); finish(); OnActivityResult returns the result From setResult with the first parameter corresponding to the second onActivityResult parameter.Copy the code

Use of onActivityResult and setResult methods in Android

If you want to get data back from a new Activity after it closes, you need to start the new Activity using the startActivityForResult(Intent Intent,int requestCode) method provided by the system. When the new Activity is closed, it returns data to the previous Activity. To retrieve the returned data, You must override the onActivityResult(int requestCode, int resultCode,Intent data) method in the previous Activity:

public class MainActivity extends Activity { @Overrideprotected void onCreate(Bundle savedInstanceState) { Button button  =(Button)this.findViewById(R.id.button); Button. SetOnClickListener (new View. An OnClickListener () {/ / clicking this button will open a new Activity publicvoid onClick (View v) {/ / the second parameter for the request code, StartActivityForResult (new Intent(mainActivity.this, newActivity.class), 1); }}); } // The first argument is the request code, that is, the value passed by the startActivityForResult() call. @override protected voidonActivityResult(int requestCode, int resultCode, Intent data) {String result = data.getextras ().getString(" result ")); // Get the data returned after the new Activity closes}}Copy the code

When the new Activity closes, the data returned by the new Activity is delivered via the Intent. The Android platform calls the onActivityResult() method of the previous Activity, passing the Intent containing the returned data as the third input parameter. Use the third input parameter in the onActivityResult() method to retrieve the data returned by the new Activity.

Request code functions:

To open a new activity using the startActivityForResult (Intent Intent.intrequestCode) method, we need to pass a requestcode to that method. The value of the request code is customized according to the business needs.

Used to identify the source of the request for example: An Activity has two buttons, and clicking on either button will open the same Activity. No matter which button opens a new Activity, when the new Activity is closed, The onActivityResult(int requestCode, int resultCode, Intent Data) method is called. In the onActivityResult() method, if you need to know which button is used to start the new Activity and do the corresponding business processing, you can do this:

@Override public void onCreate(Bundle savedInstanceState) { .... button1.setOnClickListener(newView.OnClickListener(){ public void onClick(View v) { startActivityForResult (newIntent(MainActivity.this, NewActivity.class), 1); }}); button2.setOnClickListener(newView.OnClickListener(){ public void onClick(View v) { startActivityForResult (newIntent(MainActivity.this, NewActivity.class), 2); }}); @Override protected voidonActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode){ case 1: // The request from button 1 is processed as break; Case 2: // Request from button 2, do the corresponding business process break; }}Copy the code

The result code is used to identify the source of the returned result. Within an Activity, you might use the startActivityForResult() method to open several different activities to handle different services. When these new activities are closed, The onActivityResult(int requestCode, int resultCode, Intentdata) method of the previous Activity is called. To know which NewActivity the returned data is from, do this in the onActivityResult() method (ResultActivity and NewActivity are the new activities to open) :

public class ResultActivity extends Activity {  
       .....  
       ResultActivity.this.setResult(1, intent);  
       ResultActivity.this.finish();  
}  
public class NewActivity extends Activity {  
       ......  
        NewActivity.this.setResult(2,intent);  
        NewActivity.this.finish();  
}  
public class MainActivity extends Activity { // 在该Activity会打开ResultActivity和NewActivity  
       @Override protected voidonActivityResult(int requestCode, int resultCode, Intent data) {  
              switch(resultCode){  
                  case 1:  
                      // ResultActivity的返回数据  
                      break;
                  case 2:  
	                  // NewActivity的返回数据  
	                  break;
               }  
          }  
}  
Copy the code

To return data or results, use startActivityForResult (Intent Intent, intrequestCode). The value of the requestCode is customized to identify the target Activity of the jump.

All the target Activity does is return data/result. Either setResult(int resultCode) returns only the result with no data, or setResult(int resultCode, Intent data) returns both! The handler that receives the returned data/result is onActivityResult(intrequestCode, int resultCode, Intent Data) Here the requestCode is the startActivityForResult requestCode, resultCode is the setResult in the resultCode, return data in the data!

Share with you

I want to work my way up

Fly forward on the blades at the highest point

Let the wind blow dry tears and sweat

I want to work my way up

Waiting for the sun to watch its face

Little days have big dreams

I have my day

Let the wind blow dry tears and sweat

One day I will have my day