oscarruz oscarruz - 3 months ago 20
Android Question

Intents - startActivityForResult

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?



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:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == REQUEST_CODE){
        String opcio = data.getStringExtra("extra);