naji kun
naji kun

Reputation: 167

How can I make multiple startActivityForResult to other Activity?

In my MainActivity, I hava code 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 can I set the result to 4 if it is started from condition2 with 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'm pretty new to intent,so please teach me how to solve this problem.Thank you ;)

Upvotes: 4

Views: 2887

Answers (2)

Maheshwar Ligade
Maheshwar Ligade

Reputation: 6855

It just simple just pass one more parameter (inWhichCondition) in Intent & when you send the result back just put the value you are passed. Below is the sample code.

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
        }
}

& when you set the Result back

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

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

Upvotes: 3

Kuffs
Kuffs

Reputation: 35661

If I have understood you correctly, try something like this for your conditions:

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

The just read the extra and return it as necessary.

It should be noted that resultCode is usually either -1 (RESULT_CANCELED) or 0 (RESULT_OK) which indicate failure or success. You should really return extra data as Extras in the returned intent if you wish to pass data from the second activity back to the first.

Upvotes: 3

Related Questions