Android onActivityResult 中的请求代码错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10564474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wrong requestCode in onActivityResult
提问by Dimanoid
I'm starting a new Activity from my Fragment with
我正在从我的 Fragment 开始一个新的 Activity
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 requestCodeI've just posted to startActivityForResult().
问题是我从来没有收到requestCode我刚刚发布到的startActivityForResult().
I got something like 0x40001, 0x20001etc. with a random higher bit set. The docs don't say anything about this. Any ideas?
我得到了类似的东西0x40001,0x20001等等。随机设置了更高的位。文档对此没有任何说明。有任何想法吗?
回答by Changwei Yao
You are calling startActivityForResult()from your Fragment. When you do this, the requestCodeis changed by the Activitythat owns the Fragment.
您正在startActivityForResult()从您的Fragment. 当你这样做时,requestCode通过改变Activity拥有的Fragment。
If you want to get the correct resultCodein your activity try this:
如果你想resultCode在你的活动中得到正确的尝试:
Change:
改变:
startActivityForResult(intent, 1);
To:
到:
getActivity().startActivityForResult(intent, 1);
回答by Ashlesha Sharma
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. The fragment index is later used to find the correct fragment to deliver the result.
请求代码没有错。使用 v4 支持库片段时,片段索引编码在请求代码的前 16 位中,而您的请求代码在后 16 位中。片段索引稍后用于查找正确的片段以传递结果。
Hence for Activities started form fragment object, handle onActivityResult requestCode like below:
因此,对于活动开始的表单片段对象,处理 onActivityResult requestCode 如下:
originalRequestCode = changedRequestCode - (indexOfFragment << 16)
6 = 196614 - (3 << 16)
回答by Jaime Agudo
Easier:
更轻松:
Java:
int unmaskedRequestCode = requestCode & 0x0000ffff
爪哇:
int unmaskedRequestCode = requestCode & 0x0000ffff
Kotlin:
val unmaskedRequestCode = requestCode and 0x0000ffff
科特林:
val unmaskedRequestCode = requestCode and 0x0000ffff
Check for the lower 16 bits, just unmask it doing a logical AND with the upper 16 bits zeroed
检查低 16 位,只需将其取消屏蔽,执行逻辑 AND 并将高 16 位清零
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final int unmaskedRequestCode = requestCode & 0x0000ffff
if(unmaskedRequestCode == ORIGINAL_REQUEST_CODE){
//Do stuff
}
}
回答by Nilesh Tiwari
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);
回答by TarikW
You can also definesuper.onActivityResult(requestCode, resultCode, data)
in Activity(if you overrideonActivityResult)
at this
您还可以定义super.onActivityResult(requestCode, resultCode, data)
在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
并startActivityForResult(intent, requestCode)在您的内部调用Fragment
回答by Null Pointer Exception
in Fragment
在片段中
getActivity().startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST);
in Main Activity:
在主要活动中:
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
//what ever you want to do
}

