2

I have got a contact picking method that uses two extras to specify whether you are creating a new contact shortcut in my app or editing an existing one.

Problem is that no matter what I do, the extras always seem to be null, causing a nullPointerException when I try to access them.

I suspect that setting the intent type to ContactsContract.Contacts.CONTENT_TYPE resets any user-defined extras, but I am not sure.

Here is what I am doing at the moment:

Contact Picking Intent

 Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
                Bundle extras = new Bundle();
                extras.putBoolean("isEditing", isEditing);
                extras.putLong("shortcut", shortcut.getId());
                intent.putExtras(extras);
                startActivityForResult(intent, PICK_CONTACT_REQUEST);

And in onActivityResult()

switch (requestCode) {
            case (PICK_CONTACT_REQUEST):
                if (resultCode == getActivity().RESULT_OK) {
                    Uri contactData = data.getData();
                    Bundle bundle = data.getExtras();
                    Boolean isEditing = false;
                    long shortcutId = 0;
                    if (bundle != null) {
                        Toast.makeText(getActivity(), "Bundle Not Null", Toast.LENGTH_SHORT).show();
                        if (bundle.containsKey("isEditing")) {
                            isEditing = bundle.getBoolean("isEditing", false);
                            Toast.makeText(getActivity(), "" + isEditing, Toast.LENGTH_SHORT).show();
                        }
                        if (bundle.containsKey("shortcutId")) {
                            shortcutId = bundle.getLong("shortcutId", 0);
                            Toast.makeText(getActivity(), "" + shortcutId, Toast.LENGTH_SHORT).show();
                        }
                    }

The toasts are never shown because the bundle is not found I guess. I have tried a different workflow too, using Intent.putExtraString("isEditing", value) and then getting them with intent.getStringExtra("isEditing", false) skipping packing the extras in a bundle (haven't quite understood the difference between the two workflows) but alas, I faced the same problem.

Any help would be greatly appreciated.

YFeizi
  • 1,498
  • 3
  • 28
  • 50
leoncvlt
  • 385
  • 2
  • 5
  • 13

1 Answers1

-1

Edit: When you put your long value, you do

extras.putLong("shortcut", shortcut.getId());

So you have to retrieve it like

long shortcutId = bundle.getLong("shortcut", 0);

Then you have to get the Bundle extras from the Intent that launched the Activity, like below.

switch (requestCode) {
    case (PICK_CONTACT_REQUEST):
        if (resultCode == getActivity().RESULT_OK) {
            Uri contactData = data.getData();
            //Here you get the intent extras
            Bundle bundle = getIntent().getExtras();
            //Boolean isEditing = false;
            //long shortcutId = 0;
            if (bundle != null) {
                Toast.makeText(getActivity(), "Bundle Not Null", Toast.LENGTH_SHORT).show();
                if (bundle.hasExtras()){
                    boolean isEditing = bundle.getBoolean("isEditing", false);                            
                    long shortcutId = bundle.getLong("shortcut", 0);
                    if(isEditiong != null)
                        Toast.makeText(getActivity(), "" + isEditing, Toast.LENGTH_SHORT).show();
                    if(shortcutId != null)
                        Toast.makeText(getActivity(), "" + shortcutId, Toast.LENGTH_SHORT).show();
                }
            }
        }            
    }
    break;
}
Marcus
  • 6,697
  • 11
  • 46
  • 89