android-intent data return

Sometimes data needs to be returned after jumping to another interface operation, which is data return.
1. The first activity, use startactivityforresult to jump.
The first parameter is the intent, and the second parameter is the request code

Intent intent = new Intent(MainActivity.this,MainActivity2.class);

Second, the second activity returns data after the completion of the operation and closes the current page.
Instantiate an intent to
send data
with the setresult method. Two parameters, the first is the result code, the second is the intent

Intent intent = new Intent();

Third, rewrite the onActivityResult method in the first activity.
First, judge the request code. Determine where the logic is for data return.
Then judge the different result codes, and get the corresponding data according to the different result codes returned

     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
         if (requestCode==1){
             if (resultCode==2)
                 result = data.getStringExtra("result");
             }else if (resultCode==3)
                 result = data.getStringExtra("result");

Guess you like