0

I have created an intent as follows:

I am trying to use the contacts db to store new contacts I create in my app. But I am having a tough time with invocation of

protected void onActivityResult(int requestCode, int resultCode, Intent data)

...Does not get called

    Step 1: Created intent in Activity 1



            Intent insertIntent = new Intent(ContactsContract.Intents.Insert.ACTION);
            insertIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE);


            // Sets the special extended data for navigation
            insertIntent.putExtra(ContactsContract.Intents.Insert.NAME, name);

            if (Integer.valueOf(Build.VERSION.SDK_INT) > 14) {
                insertIntent.putExtra("finishActivityOnSaveCompleted", true);
            }

            /*
            * * Demonstrates adding data rows as an array list associated with the DATA key
            * */

            // Defines an array list to contain the ContentValues objects for each row
            ArrayList<ContentValues> contactData = new ArrayList<ContentValues>();

            /*
            * * Defines the raw contact row
            * */

            // Sets up the row as a ContentValues object
            ContentValues rawContactRow = new ContentValues();


            // Adds the account type and name to the row
            rawContactRow.put(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.mydomain.com");
            rawContactRow.put(ContactsContract.RawContacts.ACCOUNT_NAME, contactName);


            // Adds the row to the array
            contactData.add(rawContactRow);


            /*
            * * * Adds the array to the intent's extras. 
            * */
            insertIntent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, contactData);



         if (isHoneycomb()) {          
            insertIntent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, parcels);

        }

CALL STARTACTIVITYFOR RESULT

 static final int INSERT_CONTACT_REQUEST = 2;

 startActivityForResult(insertIntent, INSERT_CONTACT_REQUEST);

Step 2 Called startActivityforResult() from Activity1. The app now creates the contact which I confirmed, but after completing it doesn't seem to call this method below defined in Activity1.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.i ("onActivityResult", "enter the routine!");

    // Check which request we're responding to
    System.out.println("result requestCode" + INSERT_CONTACT_REQUEST);

    Uri contactUriTry = data.getData();
    String contactUriString = contactUriTry.toString();
    System.out.print(contactUriString);

I don't have noHistory=true etc...

Any pointers where I could be going wrong?

Sudhakar R
  • 597
  • 7
  • 18
  • Maybe you find this useful: https://stackoverflow.com/questions/14278587/insert-a-new-contact-intent – Shuwn Yuan Tee Jan 23 '18 at 03:20
  • do you have a launchMode configured for your activity (https://developer.android.com/guide/topics/manifest/activity-element.html#lmode) ? – marmor Jan 23 '18 at 07:14
  • you are not calling startActivityForResult – Ishan Fernando Jan 23 '18 at 07:21
  • Sorry forgot to mention that in the code; static final int INSERT_CONTACT_REQUEST = 1; I do have the "startActivityForResult(intent, INSERT_CONTACT_REQUEST);" actually – Sudhakar R Jan 23 '18 at 08:21
  • No I have no launch mode setup. I saw some posts here that discussed the impact of launch mode, so I know that's isn't the issue. – Sudhakar R Jan 23 '18 at 08:21
  • I am not picking, i am trying to get the contact lookup key after insertion // Creates a new intent for sending to the device's contacts application Intent insertIntent = new Intent(ContactsContract.Intents.Insert.ACTION); – Sudhakar R Jan 23 '18 at 09:54

1 Answers1

0

I finally found the source of the problem.

the calling activity where onActivityResult() was being invoked, had a finish(), so even before the callback could arrive it was getting closed. So that is why I never saw the method being invoked.

After removing the finish() and moving to the last line in

onActivityResult() {
....
finish();
} 

solved the issue.

Sudhakar R
  • 597
  • 7
  • 18