The third action is called returning its result to the first activity instead of sending the result to the second action?

Scenario:

  • First activity starts seconds activity with startActivityForResult
  • The second activity starts the third activity using startActivityForResult

Expected Result: The third (last) activity sets the result that falls on the activity of the second onActivityResult

Current result: The third (last) activity sets the result that falls on the activity first onActivityResult

How can I set the result for the third activity that will be caught in the second activity. The third (last) activity sets the result that falls on the second onActivityResult activity?

Some snippets of code:

First activity

public class TestProjActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

    Log.i("TAAAG", "1st activity - startActivityForResult");
    Intent intent = new Intent(TestProjActivity.this, Activ2.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
    startActivityForResult(intent, 1008);
}

@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    super.onActivityResult(requestCode, resultCode, data);
    Log.i("TAAAG", "1st activity - onActivityResult");
    if (resultCode == RESULT_OK) {  
        Log.i("TAAAG", String.valueOf(requestCode));
        switch (requestCode) {  
        case 1008:
            String info = data.getExtras().getString("KEY1");
            Log.i("TAAAG", "1st activity - onActivityResult - printing result");
            Log.i("TAAAG", info);
            break;
        }
    }
}

}

Second activity

public class Activ2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.i("TAAAG", "2nd activity - startActivityForResult");
    Intent intent = new Intent(Activ2.this, Activ3.class);

    startActivityForResult(intent, 1009);

    Intent intent2 = new Intent();
    intent2.putExtra("KEY1", "VALUE1");


    setResult(RESULT_OK, intent2);
    finish();
}

@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    super.onActivityResult(requestCode, resultCode, data);
    Log.i("TAAAG", "2nd activity - onActivityResult");
    if (resultCode == RESULT_OK) {  
        Log.i("TAAAG", String.valueOf(requestCode));
        switch (requestCode) {  
        case 1009:
            String info = data.getExtras().getString("KEY2");
            Log.i("TAAAG", "2nd activity - onActivityResult - printing result");
            Log.i("TAAAG", info);
            break;
        }
    }
}

}

Third activity

 public class Activ3 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.i("TAAAG", "3rd activity - Inserting Value and finishing");

    Intent intent = new Intent();
    intent.putExtra("KEY2", "VALUE2");

    setResult(RESULT_OK, intent);
    finish();
}

}

+5
source share
2 answers

I think you should delete finish();in oncreate the last line in the second activity ....

+2
source

use activity flags to send the result to super activity:

intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
+8
source

All Articles