Search code examples
androidandroid-intentcontactsonactivityresult

Add Contact does not return data onactivityresult method for some Android devices


I'm messing with the following code to make it run on all the devices. Fortunately, it works perfectly fine on LG Nexus 5 (Android 6.0.1)

The same piece of code I'm trying on Sony Xperia D6503 (Android 6.0.1). But it's not working.

private void addContactUsingIntent() {
        Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        intent.putExtra(INTENT_KEY_FINISH_ACTIVITY_ON_SAVE_COMPLETED, true);
        //ContactsListActivity.this.startActivityForResult(intent, INSERT_CONTACT_REQUEST);
        startActivityForResult(intent, INSERT_CONTACT_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == INSERT_CONTACT_REQUEST) {
            if (resultCode == RESULT_OK) {
                Uri contactData = data.getData();

                Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
                if (cursor.moveToFirst()) {
                    long newId = cursor.getLong(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                    String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

                    String email = getEmail(String.valueOf(newId));

                    ArrayList<String> phones = new ArrayList<>();

                    Cursor cPhones = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                            new String[]{String.valueOf(newId)}, null);

                    while (cPhones.moveToNext()) {
                        phones.add(cPhones.getString(cPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                    }

                    cursor.close();
                    cPhones.close();

                    Uri photoUri = getPhotoUri(newId);
                    Log.i("Photo URI", photoUri.toString());

                    updateViews(newId, name, phones, email, photoUri);
                }
            }
        }
    }

Please have a look at the above code and let me know if I'm doing anything wrong.

On LG Nexus 5 device, when my app calls addContactUsingIntent the default Contact application of the device opens. As soon as users saves the contact, I'm getting data in onActivityResult. On Sony Xperia device, the default Contact screen to add contact also opens, but after users saves it, it displays the newly added contact and then user has to manually press back to return to my application. Doing so, when onActivityResult" is called, it comes back with data as null andresultCodeas-1`, which is obviously not the case.

I've also tried the following solution, but it's not working on this device. Add contact intent doesn't return data onActivityResult under ICS

Any suggestions to make it work will be greatly appreciated.

Thanks

P.S. I've also tested this code on Oppo F3 Plus (Android 6.0.1) and it's working perfectly fine.


Solution

  • The problem lies in the app that is coming up for your Intent on your Sony XPERIA device. They obviously did not test ACTION_INSERT very well (though your INTENT_KEY_FINISH_ACTIVITY_ON_SAVE_COMPLETED is not part of the Android SDK and is likely to be ignored).

    However, there is nothing that you can do about this, unless you are a Sony employee and can somehow fix the bug in their contacts app.