4

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.

Community
  • 1
  • 1
Fullplate
  • 69
  • 1
  • 7
  • See this question: http://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment It has many answers, one might be the right one for you. – lenooh Sep 04 '16 at 21:44

1 Answers1

0

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.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Hmm, so I guess that means passing a reference to my main activity, and implementing onActivityResult through the chain of Activity -> fragment -> fragment. I will see if I can get this working. – Fullplate Mar 17 '14 at 05:26
  • Just call `getActivity()` in your Fragment and it should all just work. – ianhanniballake Mar 17 '14 at 05:27
  • Oh, re your second paragraph, I don't understand... "or the Fragment's onActivityResult call will occur" -- isn't this what I am trying to achieve? *confused* – Fullplate Mar 17 '14 at 05:27
  • Ah, that seems much easier. Thanks! – Fullplate Mar 17 '14 at 05:28
  • Forgot a 'not' in that last sentence. Edited – ianhanniballake Mar 17 '14 at 05:28
  • @Fullplate: can you add working code in the question? – Trung Nguyen Aug 22 '14 at 07:04
  • @Jul I ended up leaving this position before implementing the answer. So no working code. In hindsight it seems fairly simple though. You do your startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0)); and pass in your parent activity as the first arg. Override onActivityResult in the fragment and you should receive the result. – Fullplate Aug 26 '14 at 01:32
  • @Fullplate I currently do `((InAppPurchaseFragment)fragment).onActivityResult(requestCode, resultCode, data);` in ParentActivity's `onActivityResult` . So my fragment got intent. But i'm not sure that is the right solution – Trung Nguyen Aug 26 '14 at 01:47
  • @Fullplate: My case is similar with yours. I got parent activity start PaymentFragment( consist of many payment methods). That fragment start InAppPurchase Fragment. – Trung Nguyen Aug 26 '14 at 01:50
  • According to the answer the fragment should get onActivityResult called automatically, but if that way works then it should be fine! – Fullplate Aug 26 '14 at 02:29
  • 14
    This answer is wrong, according to the source file it links. FragmentActivity does not pass onActivityResult to all attached fragments. It passes it to exactly one fragment by using the upper 16 bits of requestCode to store the calling fragment's index, but only if you called startActivityForResult() from the fragment, not the activity. Since the question deals with startIntentSenderForResult() which is only available on Activity, and derived Fragments cannot query their own index, there is no way to call Activity.startIntentSenderForResult and receive the results in Fragment.onActivityResult – foo64 Jan 07 '15 at 00:21