FLAG_ACTIVITY_CLEAR_TOP and onActivityResult

I have several actions that control the connection ( B => C => D ). If this connection fails, they should all clear and return the 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); 

B and 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 expected? Alternatively, is there a better way to achieve the same result?

+6
source share
1 answer

I suggest passing the result back to the stack instead of using Intent.FLAG_ACTIVITY_FORWARD_RESULT , so that every new Activity B, C, D is launched for the result and every Activity implemented by onActivityResult and simply sends the result down, for example:

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); setResult(resultCode); finish(); } 

Thus, the code of the expected result will return to your activity A, regardless of which one was created (B, C, D).

+1
source

Source: https://habr.com/ru/post/953013/


All Articles