I'm following the answer https://mcmap.net/q/167632/-how-to-call-android-contacts-list to add a contact picker to my app.
The problem is that onActivityResult
is immediately invoked with the correct reqCode
(PICK_CONTACT
), but with aresultCode
of 0
and a null
for data.
It is not invoked again, when the user actually picks a contact.
The AndroidManifest gives this activity android:launchMode="singleInstance">
as I only ever want there to be one instance.
What have I done wrong?
MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
addContactButton = (Button) findViewById(R.id.addContactButton);
addContactButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult");
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact name.
Log.d(TAG, "you chose " + name + ".");
}
}
break;
}
}
android:launchMode="singleInstance"
just for testing ? – UntostartActivityForResult
and its straight forward.. however the only addition you have is the"singleInstance"
so it was just a lucky guess. – Unto