开发者

Activity result not being remembered?

开发者 https://www.devze.com 2023-03-10 18:41 出处:网络
I have the following scenario (note that activity A has launchMode=\"singleTop\"): Activity A calls startActivityForResult() on activity B;

I have the following scenario (note that activity A has launchMode="singleTop"):

  1. Activity A calls startActivityForResult() on activity B;
  2. Activity B calls startActivity() on C, after which setResult(RESULT_OK) and finish(); at this point, onActivityResult() in A is NOT called.
  3. Activity C calls startActivity() on A using FLAG_ACTIVITY_CLEAR_TOP and finish();

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消