Search code examples
androidxamarinonactivityresult

OnActivityResult not called on device


I try to select contact from contacts list:

contactsButton.Click += (object sender, EventArgs e) => 
            {
                //Create a new intent for choosing a contact  
                var contactPickerIntent = new Intent(Intent.ActionPick, Android.Provider.ContactsContract.Contacts.ContentUri);
                //Start the contact picker expecting a result  
                // with the resultCode '101'  
                StartActivityForResult(contactPickerIntent, 101);
            };

and method OnActivityResult called under xamarin debugger and not called on my real device (SGS4). When user cancels operation in contacts list, method OnActivityResults called under debugger and on real device.

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            phoneNumberText.Text = String.Format("request={0}, result={1}", requestCode, resultCode);
            base.OnActivityResult(requestCode, resultCode, data);
            ...
         }

update: I don't understand why, but next line solves the issue: contactPickerIntent.SetType(ContactsContract.CommonDataKinds.Phone.ContentType);

    contactsButton.Click += (object sender, EventArgs e) => 
    {
        //Create a new intent for choosing a contact  
        var contactPickerIntent = new Intent(Intent.ActionPick, Android.Provider.ContactsContract.Contacts.ContentUri);
        contactPickerIntent.SetType(ContactsContract.CommonDataKinds.Phone.ContentType);//(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
        //Start the contact picker expecting a result  
        // with the resultCode '101'  
        StartActivityForResult(contactPickerIntent, 101);
    };

Solution

  • line contactPickerIntent.SetType(...) somehow fixes the issue.

        contactsButton.Click += (object sender, EventArgs e) => 
        {
            //Create a new intent for choosing a contact  
            var contactPickerIntent = new Intent(Intent.ActionPick, Android.Provider.ContactsContract.Contacts.ContentUri);
            contactPickerIntent.SetType(ContactsContract.CommonDataKinds.Phone.ContentType);//(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
            //Start the contact picker expecting a result  
            // with the resultCode '101'  
            StartActivityForResult(contactPickerIntent, 101);
        };