Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

android - startActivityForResult and Intents Extras, it seems extras are not pushed back

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...