Question

As per this question:

Calling startIntentSenderForResult from Fragment (Android Billing v3)

Calling startIntentSenderForResult requires you to pass an Activity that implements onActivityResult(...). Fragments cannot implement this, so if you want to call the method from one, you have to pipe it through an Activity, presumably the Activity that created the Fragment.

My issue differs from the existing question in that I do not have an easily accessible Activity that I can implement onActivityResult on. My fragment is created by another fragment, and there is a lot of logic and domain object decoding etc. that occurs between that fragment and the "main" Activity that initiates the process.

So, for practical and architectural reasons, I'd really prefer not to pipe the callback into that Activity and back down into the calling fragment.

I am thinking of something like:

  • fragment who performs the call creates a temporary Activity which contains a reference to the fragment and implements the onActivityResult, which handles the callback and routes back into the fragment.

Help of any kind will be greatly appreciated.

Was it helpful?

Solution

The default behavior for an Activity (as seen in the FragmentActivity source for onActivityResult) is to pass onActivityResult to all attached Fragments' onActivityResult in addition to the Activity's onActivityResult. Therefore, just handle the onActivityResult in your Fragment.

Note: if your Activity does handle onActivityResult, ensure that it calls super.onActivityResult or the Fragment's onActivityResult call will not occur.

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