startActivityForResult and Intents Extras, it seems extras are not pushed back
Asked Answered
E

4

20

I have this code:

Intent intent = new Intent();
        intent.setAction(Intent.ACTION_PICK);
        intent.setData(ContactsContract.Contacts.CONTENT_URI);
        intent.putExtra(EXTRA_ONLINE_ID, (String) v.getTag());
        startActivityForResult(intent, PICK_CONTACT);

Then on response:

public void onActivityResult(int reqCode, int resultCode, Intent data) {


        switch (reqCode) {
            case (PICK_CONTACT):
                if (resultCode == Activity.RESULT_OK) {
                    try {
                        Uri contactData = data.getData();
                        String onlineid = data.getStringExtra(EXTRA_ONLINE_ID);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
        super.onActivityResult(reqCode, resultCode, data);
    }

the onlineid variable is null. How can I pass a value and then to receive it back?

EDIT

I even tried,

Bundle extras = data.getExtras(); // returns null
Encage answered 18/5, 2010 at 18:12 Comment(2)
check resultCode must be -1 for Activity.RESULT_OKPepita
that is fine, it gets into that if, but the Extras is empty.Encage
T
6

This is done by design; system activities will not send back the extras with which they're called, so you have to manage the data elsewhere.

Luckily, the resultCode parameter is fully controlled by yourself, which means that you can use it to index your data.

private final int PICK_CONTACT = 0;
private Bundle[] myDataTransfer = { null };

...

Bundle myData = new Bundle();
myData.putString(EXTRA_ONLINE_ID, (String) v.getTag());
myDataTransfer[PICK_CONTACT] = myData;
// create intent and all
startActivityForResult(intent, PICK_CONTACT);

...

public void onActivityResult(int reqCode, int resultCode, Intent data) {
    if (resultCode == PICK_CONTACT) {
        Bundle myData = myDataTransfer[resultCode];
        String onlineid = myData.getString(EXTRA_ONLINE_ID);
    }
}

I'm not a Java programmer, there must be a nicer way to implement a map of Bundles, but this works :)

Tillo answered 18/4, 2013 at 3:28 Comment(4)
When you start an activity, your activity may get killed. So the index will be gone. I think you’ll need to persist the Bundle somewhere outside your App’s memory.Geotaxis
To add to the comment by @rumpel, you can test this by going into Settings > Developer Options and enabling "Don't keep activities". This way you'll be able to test this usecase and find that the data in myDataTransfer will get wiped out unless you save / restore manually in onSaveInstanceStateArundel
this doesn't even work in the best case scenario because in you mixed requestCode and resultCode. The correct signature is: onActivityResult(int requestCode, int resultCode, Intent data) And the requestCode will always be PICK_CONTACT, and the resultCode will usually be Activity.RESULT_OK==-1.Erick
@Erick it’s been a long time since I touched this answer; you’re probably right, in which case you could consider writing a new answer when you find out or edit this one :)Awad
P
0

ok Check if your Activity android:launchMode is configured as SingleTask or SingleInstance! that must be the problem :)

Pepita answered 18/5, 2010 at 18:52 Comment(2)
Hi Pentium10, did you solve this? I think everything is ok in your code :SPepita
I did not resolved. I think that the custom extras we pass to that other activity is not retained.Encage
O
0

The EXTRA_ONLINE_ID field will have to be set in the activity that you launched using setResult. If it's not setting that value in the returned Intent (which is different from what you sent) then you will get a null value.

Operate answered 15/3, 2011 at 7:5 Comment(2)
As you see it uses ContactsContract.Contacts.CONTENT_URI that is not part of my code base.Encage
Can't help it then I guess. Questions is, why do you want to return the data that you already have.Operate
P
-1

I was running into some problems with this as well.

Instead of this line

 intent.putExtra(EXTRA_ONLINE_ID, (String) v.getTag());

Try

 intent.putExtra(EXTRA_ONLINE_ID, "" + v.getTag());
Popularity answered 4/8, 2010 at 5:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.