Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Third activity called returns its result to the first activity instead of sending the result to the second activity?

Scenario:

  1. First activity starts seconds activity with startActivityForResult
  2. Second activity starts third activity with startActivityForResult

Expected result: Third (last) activity sets a result which is caught on the second activity's onActivityResult

Current result: Third (last) activity sets a result which is caught on the first activity's onActivityResult

How can I set a result on the third activity which will be caught on the second activity's Third (last) activity sets a result which is caught on the second activity's onActivityResult?

Some code snippets:

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();
}

}

like image 819
Chaiavi Avatar asked Feb 20 '12 12:02

Chaiavi


1 Answers

use Activity flags to forward result to super activity:

intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
like image 198
jeet Avatar answered Nov 04 '22 18:11

jeet