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.