16

I want my application to prompt the user to create a new contact, through the standard Contacts interface on Android. Then I want to be able to read the information back from the newly created contact.

My code is based on the "Adding A New Contact" from this site.

Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(Contacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, number);
startActivityForResult(intent, PICK_CONTACT);

and then

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Intent intent = new Intent(this, Foo.class);
    Uri uri = data.getData(); //I get nullpointer here on ICS
    intent.putExtra("contact", ContactAccessor.getInstance().loadContact(this, uri));
    startActivity(intent);
    finish();
}

This code runs fine on Android 2.2 and 2.3. It starts the contacts application and lets the user input stuff like name and email address and when they're done and hit "ok" or "save" or "whatever" it returns to my app and I can read the stuff they entered. On Android 4.0 (ICS) however it does not return back to my app, when the user is done creating the contact. And when I exit the contact view (through the back button) it does not include any intent with the contact information.

What intent should I use to get the same behavior on ICS?

Juan Gomez
  • 1,508
  • 1
  • 11
  • 21
getekha
  • 2,495
  • 3
  • 18
  • 20
  • http://stackoverflow.com/questions/9551821/android-4-0-3-and-finish-activity I am having similar issue. – user1247617 Mar 25 '12 at 03:40
  • my problem is it works if i open another activity and return back to parent activity on onActivityResult() but when i try to capture image using intent onActivityResult() called before i capture or cancel camera event. and this issue happen in android 4.03 (testing on htc me) – Hiren Dabhi Sep 25 '12 at 09:51

1 Answers1

17

For Android 4.0.3 and up, you need to provide a new intent extra:

public static final String INTENT_KEY_FINISH_ACTIVITY_ON_SAVE_COMPLETED = "finishActivityOnSaveCompleted";

intent.putExtra(INTENT_KEY_FINISH_ACTIVITY_ON_SAVE_COMPLETED, true);

I don't have a solution for Android 4.0 to 4.0.2.

Anyone?

OferR
  • 1,634
  • 19
  • 21
  • 1
    This is great! Thank you SO much. I just tried this on a Galaxy Nexus running 4.0.4 and it worked like a charm! The stupid Verizon Galaxy Nexus is still stuck on 4.0.2 so this fix still doesn't work, but hopefully Verizon will update it soon. Thanks again! – Juan Gomez Apr 25 '12 at 21:01
  • 2
    Saved me a lot of headaches, thanks! Turns out this is documented, though finding where to look in the Android docs is of course its own headache. http://developer.android.com/training/contacts-provider/modify-data.html in "Add the navigation flag" – Groxx Apr 06 '13 at 04:18
  • This does not work on Android 4.4.2. Please share if you have any solution or know what changed. Thx – Sandra Sep 16 '14 at 14:42
  • @Sandra, This works fine on my Nexus 5 and Samsung S5 running 4.4.2. Can you share more info? – OferR Sep 27 '14 at 15:42
  • I found it not working on Sony Xpera Z, Z2 and LG G2 mini, all running 4.4.2.. I asked a question here: http://stackoverflow.com/questions/25872408/incorrect-navigation-while-contact-saving-on-android-4-4-2 – Sandra Sep 29 '14 at 14:04
  • I don't have any of these phones, so I'll be watching this space. Thanks. – OferR Sep 29 '14 at 14:57