2

I make app with the MainActivity and Activity2. I have sent info from MainActivity to Activity2 and Activity2 to MainActivity.

I would like to sent the variable opcio to startActivityForResult but I do not know how. I use this code from MainActivity:

opcio = OPCIO_1;
startActivityForResult(new Intent(getApplicationContext(), Activity2.class), ACTIVITY_NUM);

After from Activity2 I do not know to recovery this info.

Please Could you help me?

Thanks.

reixa
  • 6,903
  • 6
  • 49
  • 68
ruzD
  • 555
  • 1
  • 15
  • 29
  • Possible duplicate of [How to manage \`startActivityForResult\` on Android?](http://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android) – Anil Prajapati Sep 27 '16 at 15:49
  • Possible duplicate of [How do I pass data between activities on Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android) – reixa Sep 27 '16 at 15:50
  • Possible duplicate of https://developer.android.com/training/basics/intents/result.html – Anil Prajapati Sep 27 '16 at 15:53

2 Answers2

2

How about something along these lines?

Send extra from MainActivity to Activity2:

public static final int REQUEST_CODE = 0;
Intent intent = new Intent(MainActivity.this, Activity2.class);
intent.putExtra("extra", opcio);
startActivityForResult(intent, REQUEST_CODE);  

Retrieve extra in Activity2:

String opcio = getIntent().getStringExtra("extra");

Sending extra from Activity2 to MainActivity:

Intent data = new Intent();
data.putExtra("extra", opcio);
setResult(RESULT_OK, data);

Retrieve extra in MainActivity:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == REQUEST_CODE){
        String opcio = data.getStringExtra("extra);
    }
}
Vietnt134
  • 512
  • 7
  • 19
Al Wld
  • 869
  • 7
  • 19
0

MainActivity:

Intent i = new Intent(MainActivity.this, Activity2.class);
opcio = OPCIO_1;
i.putExtra("send", opcio);
startActivityForResult(i, 1);

Activity2 where closes:

setResult(1);
finish();

MainActivity :

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == 1){
        //Your code
    }
}
Ahmet Kocaman
  • 109
  • 11
  • Ok, I understand. The little problem is that from the activity2 also I have that send information. Then, Have I create a new Intent? How do you link all information of the two intents? – ruzD Sep 27 '16 at 16:02