1

In my application I am implementing functionality to add contact. For this purpose I am using code as

Intent addNewContact = new Intent(Intent.ACTION_INSERT);
addNewContact.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(addNewContact, ADD_NEW_CONTACT);

After adding contact (on Activity Result), I want to show that contact (only, and I don't want to iterate URI), But I don't know how can I show only that contact. Is there a way?

Here is my onActivityResult()

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == ADD_NEW_CONTACT) {
             if(resultCode == -1) {
                Log.i("New contact Added : ", "Addedd new contact, Need to refress item list");
            } else {
                Log.i("New contact Added : ", "Canceled to adding new contacts : Not need to update item list");
            }
        }
    }

Thank you!

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186

1 Answers1

3

You have to read data parameter of onActivityResult(), in which you can get _id, displayName etc..

Try this code

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == ADD_NEW_CONTACT) {
             if(resultCode == -1) {
                 Uri contactData = data.getData();
                    Cursor cursor =  managedQuery(contactData, null, null, null, null);
                    if (cursor.moveToFirst()) {
                        long newId = cursor.getLong(cursor.getColumnIndexOrThrow(Contacts._ID));
                        String name = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
                        Log.i("New contact Added", "ID of newly added contact is : " + newId + " Name is : " + name);
                    }
                Log.i("New contact Added : ", "Addedd new contact, Need to refress item list : DATA = " + data.toString());
            } else {
                Log.i("New contact Added : ", "Canceled to adding new contacts : Not need to update database");
            }
        }
    }

Happy coding.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186