Android Pitfall - Fragment.startActivityForResult(), requestCode == 65536

Android里,Fragment和Activity具有 startActivityForResult() 方法,但在支持库里,二者的表现却会有很大差异。

假定目前使用的是Android支持库(Support library),我们在Fragment里,调用 startActivityForResult(intent, 0);
然后在对应的Activity里override onActivityResult()方法,我们会惊奇地发现,requestCode == 65536 (0x10000).

与此同时,如果我们在Fragment里重写(override) onActivityResult() 方法,则会得到正确的0。




察看Android的源码,可以在支持库里的 FragmentActivity找到这么一个函数:

public void startActivityFromFragment(Fragment fragment, Intent intent, 
        int requestCode) {
    if (requestCode == -1) {
        super.startActivityForResult(intent, -1);
        return;
    }
    if ((requestCode&0xffff0000) != 0) {
        throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
    }
    super.startActivityForResult(intent, ((fragment.mIndex+1)<<16) + (requestCode&0xffff));
}


这里我们可以看到,在Activity里调用时,requestCode 变成了  ((fragment. mIndex + 1 )<< 16 ) + (requestCode& 0xffff ),对比上例,fragment.mIndex 应该就是0,结果呢,requestCode 增加了65536。



再看看 FragmentActivity中的 onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    mFragments.noteStateNotSaved();
    int index = requestCode>>16;
    if (index != 0) {
        index--;
        if (mFragments.mActive == null || index < 0 || index >= mFragments.mActive.size()) {
            Log.w(TAG, "Activity result fragment index out of range: 0x"
                    + Integer.toHexString(requestCode));
            return;
        }
        Fragment frag = mFragments.mActive.get(index);
        if (frag == null) {
            Log.w(TAG, "Activity result no fragment exists for index: 0x"
                    + Integer.toHexString(requestCode));
        } else {
            frag.onActivityResult(requestCode&0xffff, resultCode, data);
        }
        return;
    }
    
    super.onActivityResult(requestCode, resultCode, data);
}

这里,根据requestCode,会将返回的数据转发给对应的Fragment





解决办法:在Fragment调用startActivityForResult(),则在该Fragment中重写onActivityResult()。
note: 同一Activity的其他Fragment也不行。




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Simultaneous, on-chip FPGA delay measurement is a powerful technique for characterizing the performance of FPGA designs. However, there are several pitfalls and tradeoffs that must be considered when using this technique. One pitfall is the potential for measurement errors due to coupling between measurement signals and other signals on the FPGA. This coupling can lead to inaccurate measurements and must be carefully controlled through proper design techniques. Another tradeoff is the tradeoff between measurement accuracy and measurement speed. More accurate measurements require longer measurement times, which can impact overall system performance. Therefore, it is important to carefully balance measurement accuracy and measurement speed to obtain the best overall system performance. Additionally, the choice of measurement technique can also impact the accuracy and speed of on-chip FPGA delay measurement. For example, pulse width measurement techniques may be faster but less accurate than time interval measurement techniques. Finally, the choice of measurement circuitry can also impact the accuracy and speed of on-chip FPGA delay measurement. Careful consideration must be given to the design of the measurement circuitry to ensure accurate and reliable measurements. Overall, simultaneous, on-chip FPGA delay measurement is a powerful technique for characterizing FPGA designs, but careful consideration must be given to the potential pitfalls and tradeoffs to obtain accurate and reliable measurements.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值