Retrieving putExtra values from onActivityResult intent Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Retrieving putExtra values from onActivityResult intent without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am trying to append certain data to an intent, before using StartActivityForResult on it.

When the intent returns in OnActivityForResult, I would like to access the data I appended in the intent. So I can correlate the data retrieved in the intent, with things like database entries, container ids, etc.

Unfortunately the intent that returns does not seem to be the same one I started. I tried comparing (==) the old and the new intent in a test case, and the result failed, and not surprisingly then the data I am trying append is not there. Is there any link back to the original intent?

Basic idea of what I’ve tried:

Code to StartActivityForResult in psuedo code:

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
i.putExtra([-Key-], [int]);
i.putExtra([-Key-], [int]);
....
getParentFragment().startActivityForResult(i, requestCode); 

Pseudo Code for OnActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

....        

    switch(requestcode){

    case RESULT_LOAD_IMAGE :

    //These always evaluate to default.  The intent returns with the picture,
    //and I process it fine (with default values), but any extra data i try to append 
    //to the intent is lost.  

    int rowId = intent.getIntExtra([-Key-], [-def_value-]);

            ....
            ....

    break;
    default:
        throw new RuntimeException();
    }
}

Answer

When you launch an Activity using implicit Intent resolution, which is what you are doing when you do this:

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
....
getParentFragment().startActivityForResult(i, requestCode);

you don’t have any guarantees what Activity will actually be chosen to perform the action. Because of this, there isn’t any “contract” between your Activity and the Activity that will be used to perform the desired action. This is, unfortunately, one of the disavantages of using implicit Intent resolution. Because there is no contract between the 2 Activities, you can’t be sure what you are going to get in the result that is returned to you in onActivityResult().

If you look at the documentation for ACTION_PICK, it at least indicates what “should” happen (if the selected Activity actually behaves the way the documentation reads):

Input: getData() is URI containing a directory of data (vnd.android.cursor.dir/*) from which to pick an item.

Output: The URI of the item that was picked.

This indicates that you should provide a URI that contains a directory of data and that you will be returned an Intent containing the URI of the item that was picked. That’s it. That’s all you can expect to get. You can put lots of other “extras” in the Intent that you pass to the Activity with ACTION_PICK, but that Activity doesn’t care about those extras and will just ignore them. The Activity that performs the ACTION_PICK will create a new Intent containing the URI of the selected item and pass that back to you. It doesn’t pass your original Intent back. The “input Intent” and the “output Intent” are completely different and don’t have anything to do with each other.

To solve your problem, I’d suggest that you create a unique integer requestCode and save your “extras” in a table or map in your Activity associated with that requestCode. Then you can launch the ACTION_PICK activity using the requestCode. In onActivityResult() you can use the requestCode argument that comes back to find your “extras” that you saved and you’ll be able to associate the returned URI with them.

NOTE: One more thing: When you call startActivityForResult() your Activity will be paused and the launched Activity will run. Your Activity won’t be resumed until onActivityResult() is called. This means that you will only ever be able to have one ACTION_PICK pending at any given time. For this reason you may not need a way to associate a specific PICK action with any given data.

We are here to answer your question about Retrieving putExtra values from onActivityResult intent - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji