How can I do multiple startActivityForResult for another activity?

In my MainActivity, I hava code looks like this:

public void toSecondActivity(View v){
        if(condition1){
            Intent it = new Intent(MainActitivy.this,SecondAcitivity.class);
            //put extra
            ......
            startActivityForResult(it,1)
        }
        else if(condition2){
            Intent it = new Intent(MainActitivy.this,SecondAcitivity.class);
            //put extra
            ......
            startActivityForResult(it,2)
        }
    }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 3) {
        //do something
        }
        else if (resultCode == 4){
        //do some other thing
        }
}

Then in SecondActivity I have this code:

public void returnToMainAcitivity(View v){
                Intent it = getIntent();
                //put extra
                .........
                it.putExtra("ResourceID", mResourceId);
                setResult(3, it);
                finish();
}

My question is: how to set the result to 4 if it is started from condition2 using the same button?

public void returnToMainAcitivity(View v){
                Intent it = getIntent();
                //put extra
                .........
                //Not 3 if started from condition2
                //setResult(3, it);
                setResult(4, it);
                finish();
}

I am new to intentions, so please teach me how to solve this problem. Thanks;)

+4
source share
2 answers

If you understand correctly, try something similar for your conditions:

Intent it = new Intent(MainActitivy.this,SecondAcitivity.class);
it.PutExtra("StartedFromCondition",1)
startActivityForResult(it,1)

Just read the additional information and return it as needed.

, resultCode -1 (RESULT_CANCELED) 0 (RESULT_OK), . , .

+3

(inWhichCondition) & ; , . .

public void toSecondActivity(View v){
        if(condition1){
            Intent it = new Intent(MainActitivy.this,SecondAcitivity.class);
            //put extra
            intent.PutExtra("inWhichCondition",3);
            startActivityForResult(it,1)
        }
        else if(condition2){
            Intent it = new Intent(MainActitivy.this,SecondAcitivity.class);
            //put extra
            intent.PutExtra("inWhichCondition",4);
            startActivityForResult(it,2)
        }
    }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 3) {
        //do something
        }
        else if (resultCode == 4){
        //do some other thing
        }
}

&

public void returnToMainAcitivity(View v){
                Intent it = getIntent();
                //put extra

                **setResult(inWhichCondition, it);**
                finish();
}
+3

All Articles