The new Android Billing v3 documentation and helper code uses startIntentSenderForResult()
when launching a purchase flow. I want to start a purchase flow (and receive the result) from a Fragment
.
For example the documentation suggests calling
startIntentSenderForResult(pendingIntent.getIntentSender(),
1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
Integer.valueOf(0));
and the helper code calls
mHelper.launchPurchaseFlow(this, SKU_GAS, 10001,
mPurchaseFinishedListener, "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
which calls startIntentSenderForResult()
.
The problem is, calling startIntentSenderForResult()
causes onActivityResult()
to be called on the parent Activity
rather than on the Fragment
that it was called from (where the IabHelper
resides).
I could receive the onActivityResult()
in the parent Activity
and then manually call the onActivityResult()
on the Fragment
, but is there a way to make a call to startIntentSenderForResult()
from a Fragment
that returns the result directly to that Fragment
's onActivityResult()
?
I suggest creating some sort of generic handling of this issue in your base activity class if you have access to it.
For example:
Of course, you'll need to implement ActivityResultHandler interface by your fragments and register them on activity startup.
RC_REQUEST
is same as you used to launch purchase flowAdd this in the
onActivityResult
of your Activity.The inventory listener will produce the desired result for you.(I know its a temp fix but worked for me)).You need to call
at the beginning of your Activity's and Fragment's onActivityResult to cascade the Results to the fragments.
In my FragmentActivity this reads as
You need to pass fragment and data to parent activity, then call fragment onActivityResult from the parent activity.
like this
in fragment:
in parent activity:
1) You should modify your resultCode (RC_REQUEST) to put fragment index to it.
2) in IabHelper.launchPurchaseFlow(...)
to