在Fragment中使用StartActivityForResult,RequestCode错乱的问题

问题:在Fragment中使用startActivityForResult(intent, 1);然后在Activity中的onActivityResult(int requestCode, int resultCode, Intent data)方法中取到 requestCode并不是1,而在Fragment中的onActivityResult方法却能获得正确的requestCode

解决:

通过搜索stackoverflow获得答案:

You are calling startActivityForResult() from your Fragment. When you do this, the requestCode is changed by the Activity that owns the Fragment.
If you want to get the correct requestCode in your activity try this:

Change:
startActivityForResult(intent, 1);
To:
getActivity().startActivityForResult(intent, 1);

    原文作者:MrRock
    原文地址: https://www.jianshu.com/p/f5da613f24f5
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞