You can finish Activity B with a result saying "start Activity C", then start Activity C for result from Activity A...
startActivityForResult from A->B->(finish B)->C and send back result from C to A
-
31-08-2022 - |
Question
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?
Solution
OTHER TIPS
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.
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()
}
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...
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
}
}
}
}