Wrong requestCode in onActivityResult
If you are providing constant make it public and then use in startActivityResult
example:
public static final int REQUEST_CODE =1;
getActivity().startActivityForresult(intent, REQUEST_CODE);
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.
Hence for Activities started form fragment object, handle onActivityResult requestCode like below:
originalRequestCode = changedRequestCode - (indexOfFragment << 16)
6 = 196614 - (3 << 16)
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 resultCode
in your activity try this:
Change:
startActivityForResult(intent, 1);
To:
getActivity().startActivityForResult(intent, 1);
Easier:
Java:
int unmaskedRequestCode = requestCode & 0x0000ffff
Kotlin:
val unmaskedRequestCode = requestCode and 0x0000ffff
Check for the lower 16 bits, just unmask it doing a logical AND with the upper 16 bits zeroed
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final int unmaskedRequestCode = requestCode & 0x0000ffff
if(unmaskedRequestCode == ORIGINAL_REQUEST_CODE){
//Do stuff
}
}