Fragment.onActivityResult is not called

Showing 1-11 of 11 messages
Fragment.onActivityResult is not called drasticp 3/9/11 8:14 AM
I have an application that targets 2.1. I'm using the Android
Compatibility Package to migrate the code in my Activities to
Fragments. I had an Activity which was launching a contact picker as
follows:

Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);

The result was appropriately handled in the onActivityResult for the
Activity:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent
data) {
        if (resultCode != Activity.RESULT_OK) return;
    switch (requestCode) {
    case CONTACT_PICKER_RESULT:
        handleResult(data);
        break;
    }
}

Now, I've migrated both the startActivityForResult call and the
onActivityResult into a Fragment. I have also extended
FragmentActivity in the hosting Activity.

The contact picker still launches correctly, but onActivityResult in
the fragment is never called. If I override onActivityResult in the
FragmentActivity, it *IS* called. However, I don't want to handle the
result there because it breaks the encapsulation philosophy of the new
fragments.

Shouldn't onActivityResult in the fragment be called? Am I missing
something? Thanks for your assistance!
Re: [android-developers] Fragment.onActivityResult is not called Dianne Hackborn 3/9/11 9:20 AM
Does the API demo for this work wherever you are running it?  I have tested it on 3.0, 2.3, and 1.6, and it works in those places, not would I expect it to have any trouble elsewhere.   (How this works is very simple, it just masks out the top X bits of the request code to determine which fragment to deliver the result to.)

Also of course if you are overriding FragmentActivity.onActivityResult(), you *do* need to be sure to call the inherited version.  The behavior here is slightly different than the HC implementation; the activity method will always be called first.


--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en



--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Re: [android-developers] Fragment.onActivityResult is not called Pete Doyle 3/10/11 1:55 AM
Ran into this issue tonight on my Droid (2.2).  I think there's an issue in FragmentActivity.startActivityFromFragment(...).

YMMV, but this seems to fix it for me:

Basically there are two issues:
  1) "(fragment.mIndex+1)<<16" should be in parentheses since + has precedence over << in Java
  2) requestCode*0xffff should be requestCode&0xffff.  (I think the goal is to strip all but the first 16 bits of the request code).

To understand the fix, assume you have a fragment index of 0 and a requestCode of 1.

With the current code:
(fragment.mIndex+1)<<16 + (requestCode*0xffff)
= (0+1)<<16 + (1*0xFFFF)
= (1)<< (16 + 0xFFFF) // since + has precedence over <<
= 1<<65551
= 32768 // according to my debugger
= 1000 0000 0000 0000 // fragment index is lost, request code changes from 1 to 32768

With this change:
((fragment.mIndex+1)<<16) + (requestCode&0xffff)
 = ((0+1)<<16) + (1&0xFFFF)
 = (1<<16) + 1
 = 65536 + 1
 = 65537
 = 1 0000 0000 0000 0001

Thanks,
Pete
Re: Fragment.onActivityResult is not called drasticp 3/10/11 7:26 AM
I think Peter's response is accurate. There seems to be a bug in the
FragmentActivity.startActivityFromFragment method.

I went back and looked at the ApiDemos code and it does work as long
as requestCode = 0. However, if you change the request code to
anything other than zero, the ApiDemos will fail.

I reported the issue here:

http://code.google.com/p/android/issues/detail?id=15394


Re: [android-developers] Fragment.onActivityResult is not called Dianne Hackborn 3/10/11 10:15 AM
Whoops, yeah that is a bug.  Thanks for finding it.  I'll fix it in the next update.
Re: [android-developers] Fragment.onActivityResult is not called Casvah 4/25/12 6:24 PM
Has this issue been fixed? The bug report on the issue tracker says it's closed, but there are new comments from people having this issue. I am also having this issue. I might be doing it wrong though, since I can't get it to work with any request code.


On Thursday, March 10, 2011 12:15:12 PM UTC-6, Dianne Hackborn wrote:
Whoops, yeah that is a bug.  Thanks for finding it.  I'll fix it in the next update.

On Thu, Mar 10, 2011 at 1:55 AM, Pete Doyle  wrote:
Re: [android-developers] Fragment.onActivityResult is not called Zsolt Vasvari 4/25/12 7:09 PM
I am sure it will be fixed in whatever the next version of Android is and if they back port the fix and the phones get updated (likelihood: 0.0001%).

So unless you are targeting ONLY the non-yet public next version of the SDK, you might as well just not even assume this fix exists and code around it somehow.
Re: [android-developers] Fragment.onActivityResult is not called Zsolt Vasvari 4/25/12 7:32 PM
Oh, I see this is from 2011, not this March.  Never mind -- the fix should certainly be part of 3.2 and ICS.
Re: [android-developers] Fragment.onActivityResult is not called Kostya Vasilyev 4/26/12 2:42 AM
The bug is in the compatibility library, not the platform, so no firmware updates are involved.

The native platform implementation of this is different.

It appears fixed in the current version of the compat library (v7?), I believe it has been for a long time.

-- K

26 апреля 2012 г. 6:32 пользователь Zsolt Vasvari <zvas...@gmail.com> написал:

Re: [android-developers] Fragment.onActivityResult is not called Casvah 4/26/12 4:21 PM
Ok, I'll verify I have the current version of the compatibility library. If it is fixed, what would cause my fragment's onActivityResult callback method to never be called?
Re: [android-developers] Fragment.onActivityResult is not called Casvah 4/26/12 5:43 PM
OK! I solved my problem. Teaches me to code when tired. I was simply missing a .show() after my toast. :(