Reputation: 111
I am trying to get a contact from contact-list but for some reason I can't get it. I have folowed some guides. This is my code:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_ice_add:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, RequestCodes.PICK_CONTACT);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
When I debug, It always goes to default case, it returns super.onOptionItemSelected(item);
@SuppressLint("InlinedApi")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// se comprueba de qué activity está volviendo
switch(requestCode) {
case (RequestCodes.PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
ContactUtils.addContactInfo(getActivity(), contactData, adapter);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
It never starts onActivityResult.
Can anybody give me a hint? I would appreciate it a lot. Thank you.
EDIT 1
I must say that this code works on Android 2.3, but it doesn't work on KitKat (those are the devices I have)
EDIT 2
I have added android.permission.READ_CONTACTS in menifest
EDIT 3 (SOLVED)
The problems was not the Intent. I am using Fragments and I was calling onActivityResult, but I didn't know I had to do "super.onActivityResult(arg0,arg1,arg2)" on the activity where my Fragment is attached.
I will not delete the question because maybe this could be helpful.
Thanxs anyway to everybody.
Upvotes: 0
Views: 435
Reputation: 1006839
When I debug, It always goes to default case, it returns super.onOptionItemSelected(item);
Then you have the wrong item ID in your case
statement, or your R
class is out of sync with your resources (clean your project, such as Project > Clean in Eclipse, to clear up that problem).
It never starts onActivityResult.
That is because you are not calling startActivityForResult()
, due to the problem with your menu resources and onOptionsItemSelected()
implementation, noted above.
Also note that you do not need READ_CONTACTS
for the uses shown in your code, though you may need it for whatever is in ContactUtils.addContactInfo()
.
FWIW, this directory contains five sample projects, all of which successfully use ACTION_PICK
for ContactsContracts.Contacts.CONTENT_URI
. The differences between the projects come down to how they handle configuration changes (e.g., screen rotation).
Upvotes: 0