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

Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
Luca
  • 823
  • 4
  • 11
  • 31

2 Answers2

30

Just replace

startActivityForResult(intent, Defines.FILTER_BY_CATALOGUE);

with

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

It will work for sure. :)

ugo
  • 2,705
  • 2
  • 30
  • 34
Nitesh
  • 3,868
  • 1
  • 20
  • 26
  • For me it is not clear why this would work. Could you explain why this will solve the issue? – dragi Dec 01 '14 at 09:38
  • Bloody hell! I don't know why but it works! Can you explain why? – Luca Dec 01 '14 at 09:41
  • 1
    I assume you are starting this intent from a fragment..? – Nitesh Dec 01 '14 at 09:43
  • here getActivity() return the context of current activity rather then entire application – Radhey Dec 01 '14 at 09:45
  • 1
    @Luca 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. – Kai May 15 '15 at 05:45
  • 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? – Haya SVU Nov 14 '20 at 18:14
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.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • @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). – laalto Aug 03 '15 at 11:40