2

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();
    }
}
NameSpace
  • 10,009
  • 3
  • 39
  • 40
  • Can you describe PLZ what is the purpose of this code? what are you trying to acheive? – Matan Dahan Jan 13 '14 at 18:52
  • Hopefully this answer can help you, http://stackoverflow.com/a/2141166/2150044 I'm on a mobile device atm, but if you still need help in an hour I'll post my reply – nstosic Jan 13 '14 at 18:54
  • The activity started lets the user select an image (from the phone, picasa, google, etc). When the intent returns, I would like to associate the intent with variables, like rowIDs in sqlite, container/frame ids, etc. I suppose I could rely on intents being launched and returned sequentially, and just dump the data in a global variable... I wasn't sure if that last assumption, about sequential start and return, would last though. – NameSpace Jan 13 '14 at 19:16

1 Answers1

7

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.

David Wasser
  • 93,459
  • 16
  • 209
  • 274