[Solved] how can we send data from first activity to second activity and second activity to first activity bu using intent [duplicate]


first, define a variable in you first activity like this (100 is just random, pick whatever you want):

private static final int SECOND_ACTIVITY = 100;

then in your first activity you start the second activity like this:

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

also override onActivityResult in your first activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SECOND_ACTIVITY) {
        if (resultCode == RESULT_OK) {
            String foo = data.getStringExtra("foo");
        }
    }
}

when you finish your second activity put you data, like this:

Intent data = new Intent();
data.putExtra("foo", "bar");
setResult(RESULT_OK, data);
finish();

solved how can we send data from first activity to second activity and second activity to first activity bu using intent [duplicate]