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()
?
if you want to get callback on your fragment than call
super.onActivityResult()
from your activity.This will call your fragments
onActivityResult()
.And don't forget to call
startIntentSenderForResult
from your fragment context.Don't use activity context
getActivity().startIntentSenderForResult
I suggest two solutions:
1.) Put the IabHelper mHelper on the activity and call the IabHelper from the fragment.
Something like:
To use this solution, Declare IabHelper as public in the activity and use a method to call the launcher from the Fragment.
2.) In onActivityResult, call the appropriate fragment that contains the IabHelper object. Appropriate fragment can have an access method to the helper object.
Regarding LEO's very helpful 2nd solution above:
If Google ever fixes the issue with startIntentSenderForResult and it now correctly routes the onActivityResult call to the fragment, then this solution should be future-proofed so that the fragment's onActivityResult doesn't get called twice.
I would like to propose the following modified solution proposed by LEO.
In the Fragment's parent Activity implementation:
Then in your IAB Fragment's implementation:
Edit:
android.support.v4.app.Fragment
now contains a backwards compatible version ofstartIntentSenderForResult()
, so this answer is obsolete.Old answer:
As of support library 23.2.0, modifying the
requestCode
no longer works:FragmentActivity
now keeps track of the requests made by its fragments. I added this method to theFragmentActivity
that was hosting theFragment
(code based onFragmentActivity.startActivityFromFragment(Fragment, Intent, int, Bundle)
):When calling this, only the passed
Fragment
will receive theonActivityResult()
call.From SDK 24 and above, there is a startIntentSenderForResult method available in support Fragment also, which works as intended. Note that there is an additional Bundle parameter, which can be passed as null. Thus, final code will be:
Of course, for API 23 and below, we will still need to use the tricks described in other answers.
In my case i did onActivityResult in Activity :
and same in fragment and it makes in app billing works