I have multiple activities that manage a connection (B => C => D). If that connection drops out, they should all clear out and return a result back to A, depending on the reason (RESULT_USER_TERMINATED, RESULT_LOW_SIGNAL, RESULT_UNKOWN
, etc...)
Intent intent = new Intent(this, B.class);
startActivityForResult(intent, REQUEST_EXIT_STATUS);
Intent intent = new Intent(this, C.class);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(myIntent);
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
setResult(ConnectActivity.RESULT_USER_TERMINATED);
startActivity(intent);
This does not work. Instead, A gets RESULT_CANCELED
. How can I make this work as expected? Alternatively, is there a better way to acheive the same result?
I suggest passing the result back down the stack instead of making use of Intent.FLAG_ACTIVITY_FORWARD_RESULT
, such that each new Activity B, C, D are started for result and each Activity implemented onActivityResult and simply forwards the result down eg:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
setResult(resultCode);
finish();
}
That way the intended result code will make it's way back to your Activity A regardless of which Activity (B, C, D) generated it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With