Dimanoid Dimanoid - 5 months ago 72
Android Question

wrong requestCode in onActivityResult

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
}
}


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?

Answer

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);
Comments