How Android returns data to the previous activity

Posted by kiju111 on Wed, 19 Jun 2019 18:16:32 +0200

How Android returns data to the previous activity

We know that to pass data from an activity to the next activity, we need to use the method putExtra(), then call getIntent() in the next activity to get the Intent used to start the current activity, and then call the method getStringExtra() to get the incoming data, so how do we return the data to the last activity?
To start the next activity first, we need to change the startActivity() method to startActivityForResult(), which takes two parameters, the first one is Intent and the second one is Request Code, which is used to determine the data source in subsequent callbacks. Here's how we want to actually verify the operation

    public class FirstActivity extends AppCompatActivity {

    private static final String TAG = "FirstActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
        Button button1 = (Button) findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                startActivityForResult(intent, 1);

            }
        });
    }
 }

The startActivityForResult() method is used to start the second activity, where the request code is a unique value and we pass in 1.Next, in the second activity, we'll add logic to return the data, noting that we called the setResult() method.This method is important because it returns data for the previous activity, which has two parameters. The first parameter is used to return processing results to the previous activity, typically RESULT_OK or RESULT_CANCELED, and the second parameter is to take Intent with the data back.

    public class SecondActivity extends AppCompatActivity {

    private static final String TAG = "SecondActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
        Button button2 = (Button) findViewById(R.id.button_2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("extra_return", "Hello FirstActivity");
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}

Finally, since we used the startActivityForResult() method to start SecondActivity, we would call back the onActivityResult() of the previous activity before the econdActivity was destroyed, so we need to rewrite this method in the first activity to get data.
So far, the three most critical methods are startActivityForResult() and onActivityResult() in the first activity and setResult() in the second activity.The first parameter of the onActivityResult method, requestCode, comes from the second parameter of startActivityForResult, and resultCode and data come from the parameters of setResult so that all parameters correspond to the method.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String extraReturn = data.getStringExtra("extra_return");
                    Log.d(TAG, extraReturn);
                }
                break;
            default:
        }
    }

Topics: Android