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

前端 未结 4 1906
鱼传尺愫
鱼传尺愫 2020-12-10 02:34

I have this code:

Intent intent = new Intent();
        intent.setAction(Intent.ACTION_PICK);
        intent.setData(ContactsContract.Contacts.CONTENT_URI);
         


        
4条回答
  •  既然无缘
    2020-12-10 03:01

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

提交回复
热议问题