I have the following scenario (note that activity A has launchMode="singleTop"
):
- Activity A calls
startActivityForResult()
on activity B; - Activity B calls
startActivity()
on C, after whichsetResult(RESULT_OK)
andfinish()
; at this point,onActivityResult()
in A is NOT called. - Activity C calls
startActivity()
on A usingFLAG_ACTIVITY_CLEAR_TOP
andfinish()
;
This is where my problem occurs. At this point, A's onActivityResult()
is called with the right requestCode
, but something else than RESULT_开发者_运维知识库OK
as the resultCode
. I was expecting it to receive RESULT_OK
because I have set it in B (which was started for result).
Why am I getting something other than RESULT_OK
?
Read this doc:
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
Passing clear top flag will finish all activities in the stack. So what you'll get in onActivityResult
is probably a "notification" that the activity for which you want the result was canceled. (aka RESULT_CANCELED = 0x00)
UPDATE
When I use startActivityForResult
, after setting the result, I always finish my activity and let the caller come to action.
You are doing something less common: you set the result, finish your activity but you also start another one. I don't know is the expected behavior in this situation. Can't you change the interaction between the activities?
You could try to call finish()
first and then start the new activity (do this in activities B and C). Anyway, I also don't know what should happen when you do this. I recommend you the first approach (changing the interaction so you don't return a result and create a new activity at the same time).
Perhaps you could chain two startActivityForResult
or your activity B could send a new intent to A instead of returning its result?
精彩评论