Question

I have a ListActivity which displays all the aggregate contacts. When the user clicks one, my code calls startActivityForResult. All this works properly. When the user finishes editing, I want my ListActivity to be displayed again. Instead, the "people" activity gets displayed. Similarly, my onActivityResult function never gets called.

Here is the code handling the click:

  @Override
  public void onItemClick (AdapterView<?> parent, View view, int position, long id) 
  {
    Cursor cur = ((SimpleCursorAdapter)parent.getAdapter()).getCursor();
    cur.moveToPosition (position);
    String key = cur.getString (2);  // "2" is the col containing the LOOKUP_KEY
    System.out.println ("clicked " + key);

    // make intent to edit contact
    Intent intent = new Intent (Intent.ACTION_EDIT);
    intent.setData (Uri.parse (ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + key));
    startActivityForResult (intent, 2);
  }

And I also have an onActivityResult function:

  @Override
  protected void onActivityResult (int requestCode, int resultCode, Intent data)
  {
    System.out.println ("request " + requestCode + ", result " + resultCode);
  }

Any suggestions?

Was it helpful?

Solution

I filed a bug to android about this. Someone looked at it and responded that there is an undocumented workaround. From the bug report:

The undocumented workaround is to call putExtra("finishActivityOnSaveCompleted", true); on the ACTION_EDIT Intent.
However, as this is undocumented, I have no idea which Android version(s) will use it.

I tried it and it works for the version of Android I'm using: 4.1.2. See issue 39262 for more info.

OTHER TIPS

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == 2) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}

Replace your onActivity result to this. for request code get after they will work

Add

super.onActivityResult(requestCode, resultCode, data);

in OnActivtyResult().

Make sure you are calling startActivityForResult from an activity, then only your onActivityResult will be called. For example, if you have the similar code in an fragment, the onActivityResult will never be called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top