Wrong requestCode returned onActivityResult from another Activity
Asked Answered
R

2

22

I have an Activity that calls another Activity, that calls some other Activities. I send to the last Activity to get a result, and then i send back the result to the fist Activity.

The flow is somthing like

A -> B -> C -> D -> C -> B -> A

With the flow from A to D is made of startActivityForResult and the flow from D to A is made of onActivityResult.

From D to B the requestCode is always the same (the one I decided), but from B to A it suddenly change from my value to a random value (in this particular case 196614).

This is the code I use to call the activity B from activity A:

filterByCatalogue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), CatalogueContainerActivity.class);
            startActivityForResult(intent, Defines.FILTER_BY_CATALOGUE);
        }
    });

(With filterByCatalogue as a FrameLayout)

This is the code I use to call back the activity A:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Defines.FILTER_BY_CATALOGUE) {
            if (resultCode == RESULT_OK) {
                Intent intent = new Intent();
                intent.putExtra("article", data.getStringExtra("article"));
                setResult(RESULT_OK, intent);
                finish();
            }
        }
    }

I've searched a lot but I can't find where I go wrong....

Renee answered 1/12, 2014 at 9:14 Comment(1)
please show your code of activity A from where you starting an intent for activity BCouloir
C
30

Just replace

startActivityForResult(intent, Defines.FILTER_BY_CATALOGUE);

with

getActivity().startActivityForResult(intent, Defines.FILTER_BY_CATALOGUE);

It will work for sure. :)

Couloir answered 1/12, 2014 at 9:33 Comment(6)
For me it is not clear why this would work. Could you explain why this will solve the issue?Easel
Bloody hell! I don't know why but it works! Can you explain why?Renee
I assume you are starting this intent from a fragment..?Couloir
here getActivity() return the context of current activity rather then entire applicationSlob
@Renee the reason is that request code from Fragments are modified before actually being sent by Activity.startActivityForResult(), so that when the Activity receives the result it would know which Fragment to deliver it to. For example, the value you got, 196614, is 0x30006, where 6 should be the original request code and 0x30000 is what was inserted by the framework.Coffeng
I have the same problem and I use this method iside activity not fragment, so the method 'getActivity()' is not resloved.. How Can I handel it?Kelt
C
12

The request code is not random. 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 to. Reference.

For example, 196614 is really 3 << 16 + 6 where 3 is the fragment index plus one and 6 is your request code.

Morale: Don't mix activity/fragment startActivityForResult() and onActivityResult(). When started from an activity, process the result in the activity. When started from a fragment, process the result in the fragment.

Cathey answered 19/5, 2015 at 19:24 Comment(1)
@VladimirIvanov The accepted answer was accepted Dec 1 2014 i.e. a long time ago. I posted my answer only much later when the question popped up due to edits and a bounty by Nitesh (that was not granted to anyone - possibly he tried to bounty his own answer).Cathey

© 2022 - 2024 — McMap. All rights reserved.