How Activity.finish() & startActivityForResult() collaborate with each other?
I got a question about Activity.finish()
.
In my Android code, there are 4 activities (A, B, C, D). The starting activity A calls startActivityForResult(intent, reqCode)
to start activity B. B starts C, and then finish()
, not waiting for result. C does exactly the same as B, starts D and then finish()
. D will return some result, by setResult(resultCode)
.
When my code runs and activity D returns some result, A will catch a RESULT_CANCELED
on onActivityResult()
, even if RESULT_OK
is returned in D. I guess the RESULT_CANCELED
is from activity B, which has been terminated before D returns a res开发者_JAVA技巧ult, rather than from D.
But, my question is, why activity A catch RESULT_CANCELED
after D returns some result, rather than immediately after B is terminated? And, what should I do to make A catch results from D? Do I have to keep B and C alive, to pass results from D to A?
From your explanation, i understood that activity A starts B starts C and C starts D.
B after starting C invokes finish()
and C also does the same.
Since B has been started by A, it will be waiting for result from B, as A has not been linked to D, it doesn't matter even if D returns some result.
If you want to get RESULT_OK
in onActivityResult()
of A, you will have to receive valid result from B.
For that you have to receive D's result in C then pass it to B then from B pass it to A.
You can invoke finish()
in onActivityResult()
of B and C after passing result back to their respective calling activities through Intent.
I think, I understood your question correctly. If its wrong, please forgive me.
Start activity B and C with flag Intent.FLAG_ACTIVITY_FORWARD_RESULT
.
May this wont be too sensible,
but why dont you **set result of B and C to RESULT_OK**
depeding on some situation evaluation. I hope
精彩评论