I am trying to get contactId of newly added contact programmatically. Contact is being added successfully and fetches the contactID from ContentProviderResult[] but contactId which I am getting is not the correct one. I have also seen this and this answer but contactId is not the correct one. Here is my code
ArrayList<ContentProviderOperation> contentProviderOperation = new ArrayList<>();
......
......
ContentProviderResult[] results = getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY, contentProviderOperation);
long contactId = ContentUris.parseId(results[0].uri);
I have also tried the below code:
ContentProviderResult[] results = getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY, contentProviderOperation);
Uri myContactUri = results[0].uri;
int lastSlash = myContactUri.toString().lastIndexOf("/");
int length = myContactUri.toString().length();
int contactID = Integer.parseInt((String) myContactUri.toString().subSequence(lastSlash+1, length));
But the contactId I am getting here is not the correct one. Please Correct me if I am going something wrong. Thanks in advance.