I have an app that gets the ContactsContract.Contacts.LOOKUP_KEY
of a contact on the device and saves it on the app Db.
After reading this page I thought I could use the LOOKUP_KEY
to uniquely identify a contact, even when a contact is edited (for example after editing the name of the contact).
Actually I saw that after editing a contact, its LOOKUP_KEY
changes, so I cannot use anymore the LOOKUP_KEY
I saved on my app DB.
My question is: is there a way to uniquely identify a contact on ContactsContract.Contacts
from when it is created for the first time on the device until it is deleted from the device?
Thank you
A LOOKUP_KEY
is not meant to be used as a key on its own, instead it should be used together with a contact's _ID
to form a full lookupUri
.
The lookupUri
can then be used to find a contact in CONTENT_LOOKUP_URI
tables.
The CONTENT_LOOKUP_URI
basically first looks for the contact by _ID
, if it fails to find it, or the _ID
seems like the wrong contact, it uses hints from the LOOKUP_KEY
part to try and track down the correct contact for you.
From CONTENT_LOOKUP_URI
A content:// style URI for this table that should be used to create
shortcuts or otherwise create long-term links to contacts. This URI
should always be followed by a "/" and the contact's LOOKUP_KEY. It
can optionally also have a "/" and last known contact ID appended
after that. This "complete" format is an important optimization and is
highly recommended.
As long as the contact's row ID remains the same, this URI is
equivalent to CONTENT_URI. If the contact's row ID changes as a result
of a sync or aggregation, this URI will look up the contact using
indirect information (sync IDs or constituent raw contacts).
Lookup key should be appended unencoded - it is stored in the encoded
form, ready for use in a URI.
From getLookupUri(long contactId, String lookupKey)
Build a CONTENT_LOOKUP_URI lookup Uri using the given _ID and
LOOKUP_KEY.
From LOOKUP_KEY
An opaque value that contains hints on how to find the contact if its
row id changed as a result of a sync or aggregation.
The row id (primary key) for each contact called _ID
.