Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FLAG_ACTIVITY_CLEAR_TOP and onActivityResult

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...)

In A I have

Intent intent = new Intent(this, B.class);
startActivityForResult(intent, REQUEST_EXIT_STATUS);

In B & C

Intent intent = new Intent(this, C.class);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(myIntent);

IN D

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?

like image 361
firyice Avatar asked Sep 03 '13 00:09

firyice


Video Answer


1 Answers

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.

like image 77
TheIT Avatar answered Nov 01 '22 06:11

TheIT