[英]Fragments startActivityForResult always return resultCode 0 and intent null on callback onActivityResult
I searched all over and there are similar posts about it, but can't find a solution! 我搜遍了所有关于它的类似帖子,但找不到解决方案!
My situation is I have an Activity A that holds a fragment
, and from that fragment
I want to start a new Activity
B that should return some values to the fragment
. 我的情况是我有一个活动A,它包含一个
fragment
,我想从该fragment
开始一个新的Activity
B,它应该向fragment
返回一些值。
On the fragment
在
fragment
startActivityForResult(mapIntent, ConstantsUtils.TOMAP_REQUEST_CODE);
On the Activity
B, to return the data 在
Activity
B上,返回数据
Intent returnIntent = new Intent();
returnIntent.putExtra(SerializationConstants.isSearchSaved, mAbItemsShown.ordinal());
setResult (ConstantsUtils.SUCCESS_RETURN_CODE, returnIntent);
finish();
On the fragment 在片段上
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case ConstantsUtils.TOMAP_REQUEST_CODE:
if (resultCode == ConstantsUtils.SUCCESS_RETURN_CODE) {
//do some stuff
}
}
}
onActivityResult
from the fragment
is successfully called, with the right requestCode
but resultCode
is always 0 and intent is always null
. 使用正确的
requestCode
成功调用fragment
onActivityResult
,但resultCode
始终为0,而intent始终为null
。
I have no other onActivityResult
implementation under Activity
A. 我在
Activity
A下没有其他onActivityResult
实现。
In fact, I also try starting the activity
from the fragment
with getActivity().startActivityForResult(mapIntent, ConstantsUtils.TOMAP_REQUEST_CODE);
其实,我也试着开始的
activity
从fragment
与getActivity().startActivityForResult(mapIntent, ConstantsUtils.TOMAP_REQUEST_CODE);
and implementing onActivityResult
on Activity
A but it happens the same, right requestCode
but wrong resultCode
and intent
. 并在
Activity
A上实现onActivityResult
,但它发生相同,正确的requestCode
但错误的resultCode
和intent
。
I'm using Sherlock Action Bar
so my fragment
is a SherlockListFragment
so I'm using the support library (r18)
. 我正在使用
Sherlock Action Bar
所以我的fragment
是SherlockListFragment
所以我正在使用support library (r18)
。
Can you help me? 你能帮助我吗? Thanks
谢谢
Result code 0 is RESULT_CANCELLED
. 结果代码0是
RESULT_CANCELLED
。
The resultCode will be RESULT_CANCELED if the activity explicitly returned that, didn't return any result, or crashed during its operation.
Also the common reason of getting this code is a launching an activity in a new task (check your intent and manifest for flags, which lead to the start of a new task). 获取此代码的常见原因是在新任务中启动活动(检查标记的意图和清单,这将导致新任务的启动)。
Also if you have a parent activity, you should set result code of it instead of set result code of its child (try to getParent
and if it is not null, than set result code of it). 此外,如果您有父活动,则应设置它的结果代码,而不是设置其子项的结果代码(尝试
getParent
,如果它不是null,则设置它的结果代码)。
I spent some time to find out the reason. 我花了一些时间找出原因。 My mistake was put
super.onBackPressed()
in the onBackPress()
method. 我的错误是把
super.onBackPressed()
在onBackPress()
方法。
I called finish()
in the method. 我在方法中调用了
finish()
。 But I think super.onBackPressed()
will call the finish()
automatically so you will get resultCode 0 always. 但我认为
super.onBackPressed()
会自动调用finish()
所以你总是得到resultCode 0 。 So just remove the super.onBackPressed()
line in the onBackPressed()
method. 所以只需删除
onBackPressed()
方法中的super.onBackPressed()
行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.