onActivityResult中的requestCode错误

本文翻译自:Wrong requestCode in onActivityResult

I'm starting a new Activity from my Fragment with 我正在从我的片段开始一个新的活动

startActivityForResult(intent, 1);

and want to handle the result in the Fragment's parent Activity: 并想在Fragment的父Activity中处理结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult, requestCode: " + requestCode + ", resultCode: " + resultCode);
    if (requestCode == 1) {
        // bla bla bla
    }
}

The problem is that I never got the requestCode I've just posted to startActivityForResult() . 问题是我从来没有收到刚发布到startActivityForResult()requestCode

I got something like 0x40001 , 0x20001 etc. with a random higher bit set. 我像0x400010x20001等具有随机更高位设置。 The docs don't say anything about this. 该文档对此没有说什么。 Any ideas? 有任何想法吗?


#1楼

参考:https://stackoom.com/question/IkiK/onActivityResult中的requestCode错误


#2楼

You are calling startActivityForResult() from your Fragment . 您正在从Fragment调用startActivityForResult() When you do this, the requestCode is changed by the Activity that owns the Fragment . 执行此操作时,拥有FragmentActivity将更改requestCode

If you want to get the correct resultCode in your activity try this: 如果要在活动中获取正确的resultCode ,请尝试以下操作:

Change: 更改:

startActivityForResult(intent, 1);

To: 至:

getActivity().startActivityForResult(intent, 1);

#3楼

If you are providing constant make it public and then use in startActivityResult 如果要提供常量,请使其公开,然后在startActivityResult使用

example: 例:

public static final int REQUEST_CODE =1;
getActivity().startActivityForresult(intent, REQUEST_CODE);

#4楼

The request code is not wrong. 请求代码没有错。 When using v4 support library fragments, fragment index is encoded in the top 16 bits of the request code and your request code is in the bottom 16 bits. 使用v4支持库片段时,片段索引编码在请求代码的高16位中,而您的请求代码则编码在低16位中。 The fragment index is later used to find the correct fragment to deliver the result. 片段索引随后用于查找正确的片段以传递结果。

Hence for Activities started form fragment object, handle onActivityResult requestCode like below: 因此,对于从表格片段对象开始的活动,请按以下方式处理onActivityResult requestCode:

originalRequestCode = changedRequestCode - (indexOfFragment << 16)
      6             =      196614        -       (3 << 16)

#5楼

You can also define 您也可以定义
super.onActivityResult(requestCode, resultCode, data)
in Activity (if you override onActivityResult ) at this Activity (如果您覆盖onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {

        ...

        default:
            super.onActivityResult(requestCode, resultCode, data);
    }
}

and call startActivityForResult(intent, requestCode) inside your Fragment 并在Fragment调用startActivityForResult(intent, requestCode)


#6楼

Easier: 更轻松:

int unmaskedRequestCode = requestCode & 0x0000ffff

Check for the lower 16 bits, just unmask it doing a logical AND with the upper 16 bits zeroed 检查低16位,只需对其进行逻辑“与”操作,并将高16位清零即可

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    final int unmaskedRequestCode = requestCode & 0x0000ffff

    if(unmaskedRequestCode == ORIGINAL_REQUEST_CODE){
      //Do stuff

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值