Add contact intent doesn't return data onActivityResult under ICS

跟風遠走 提交于 2019-11-30 01:09:01

问题


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?


回答1:


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?



来源:https://stackoverflow.com/questions/8597080/add-contact-intent-doesnt-return-data-onactivityresult-under-ics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!