错误的请求,code在onActivityResult错误、code、onActivityResult

2023-09-12 10:50:48 作者:Sweet

我开始从我的片段,一个新的活性

I'm starting a new activity from my fragment with

   startActivityForResult(intent, 1);

和要处理导致片段的父活动

and want to handle result in fragment's parent 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
    }
}

现在的问题是,我从来没有要求code我刚刚张贴到startActivityForResult。 我得到的东西像0x40001,0x20001等有了更高的随机位设置。文档不sayung这事。任何想法?

The problem is that I never got requestCode I've just posted to startActivityForResult. I got something like 0x40001, 0x20001 etc. With random higher bit set. Docs not sayung anything about this. Any ideas?

推荐答案

正在调用 startActivityForResult()片段。当你这样做时,请求code 是由活动拥有的片段。

You are calling startActivityForResult() from your Fragment. When you do this, the requestCode is changed by the Activity that owns the Fragment.

如果你想获得正确的结果code 在您的活动试试这个:

If you want to get the correct resultCode in your activity try this:

修改

startActivityForResult(intent, 1);

要:

getActivity().startActivityForResult(intent, 1);