startActivityForResult from A->B->(finish B)->C and send back result from C to A

811 views Asked by At

Activity A is at the root of the stack, then I startActivityForResult B, then startActivityForResult C from B and finish B.. therefore the stack is now A-C.

However when I setResult in C and finish it, the Activity A doesn't receive this result.. Is this possible to do?

5

There are 5 answers

0
jpm On BEST ANSWER

You can finish Activity B with a result saying "start Activity C", then start Activity C for result from Activity A...

9
Techfist On

Instead of starting activity C using Context of Activity B, start it for result using Context of Activity A itself, in this way when you set result in C and finish it will return back to A.

0
AudioBubble On

I would suggest to implement a modal where you can save the resultant Data in Activity C, and just finish Activity C so that Activity A appears on top, in onresume() of Activity A you can access the data in the Modal with some conditions and do the necessary manipulations...

0
Ali Ahmad On
class A extends Activity
{
    protected void onCreate(Bundle savedInstanceState) 
    {
            // code.........
        Button button=findViewById(R.id.btn_id);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) 
            {               
                Intent intent=new Intent(A.this,B.class);
                startActivityForResult(intent, 1001);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK )
        {
            if(requestCode==1001)
            {
                Intent intent=new Intent(A.this,C.class);
                startActivityForResult(intent, 1002);
            }
            else if(requestCode==1003)
            {
                //here you will get the result form c
            }
        }


    }

}
1
Aniket On

If you would like to get the result from Activity C passed back to Activity A:

In Activity A call B:

Intent showB = new Intent(ActivityA, ActivityB); 
startActivityForResult(showB, RequestCode); 

In Activity B call C:

Intent showC = new Intent(ActivityC);
showC.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(showC); 
finish();

In Activity C:

//set the result code and close the activity
Intent result = new Intent();
setResult(RESULT_OK, result);
finish();

In Activity A:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

  doSomeStuffIfRequestCodeMatched()
}