Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong requestCode returned onActivityResult from another Activity

Tags:

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

like image 234
Luca Avatar asked Dec 01 '14 09:12

Luca


People also ask

How do I pass onActivityResult from activity to fragment?

To get the result in your fragment make sure you call startActivityForResult(intent,111); instead of getActivity(). startActivityForResult(intent,111); inside your fragment. @StErMi Make sure you call startActivityForResult() and not getActivity(). startActivityForResult() from your fragment.

What can I use instead of Startactivity for results?

We use startActivityForResult() to send and receive data between activities, in almost of our android projects. But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.

What can I use instead of startActivityForResult in Android?

0 . It has deprecated startActivityForResult in favour of registerForActivityResult . It was one of the first fundamentals that any Android developer has learned, and the backbone of Android's way of communicating between two components.

Is onActivityResult called before onResume?

The call to onActivityResult happens before onResume, actually (see the docs).


2 Answers

Just replace

startActivityForResult(intent, Defines.FILTER_BY_CATALOGUE); 

with

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

It will work for sure. :)

like image 197
Nitesh Avatar answered Oct 03 '22 09:10

Nitesh


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.

like image 38
laalto Avatar answered Oct 03 '22 09:10

laalto