Search code examples
androidonactivityresultstart-activity

Wrong requestCode returned onActivityResult from another Activity


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


Solution

  • Just replace

    startActivityForResult(intent, Defines.FILTER_BY_CATALOGUE);
    

    with

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

    It will work for sure. :)