112

I've been trying to set up in-app billing for my first app, and have been using the android.test.purchased sku. The purchase come through, and I manage to get the SKU into my inventory, but, as the title says, onIabPurchaseFinished, is never called.

I think it may have something to do with this Log: "Couldn't save which view has focus because the focused view com.android.internal.policy.impl.PhoneWindow$DecorView@406743d0 has no id". That pops up, right before going of to Google Play. I'm not really sure what that means though...

Launching purchase:

mHelper.launchPurchaseFlow(this, sku, 10001, mPurchaseFinishedListener, "");

And the Listener:

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {

    @Override
    public void onIabPurchaseFinished(IabResult result, Purchase info) {
        System.out.println("Purchase Finish heard something");

        if (result.isFailure()) {
             Log.d(TAG, "Error purchasing: " + result);
             return;
        } else{
                Log.d(TAG,"Success!");
             }


    }
};
AAAton
  • 1,463
  • 2
  • 12
  • 11

5 Answers5

206

Try adding this to the Activity that calls mHelper.launchPurchaseFlow(..):

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);

    // Pass on the activity result to the helper for handling
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
        // not handled, so handle it ourselves (here's where you'd
        // perform any handling of activity results not related to in-app
        // billing...
        super.onActivityResult(requestCode, resultCode, data);
    }
    else {
        Log.d(TAG, "onActivityResult handled by IABUtil.");
    }
}
pix
  • 5,052
  • 2
  • 23
  • 25
bugzy
  • 7,086
  • 9
  • 42
  • 44
  • 16
    had the same issue and that worked perfect. But what i want to know is why doesnt the call back get executed like one would assume, why do we have to make the call to that our self? Kind of makes the callback useless :/ – Spider Mar 21 '13 at 01:37
  • 4
    Also helped me, thx. @Spider, the reason you need this method in your Activity is that otherwise mHelper's handleActivityResult() method won't get called. In other words, this is the real/standard handleActivityResult() needed in the Activity, and mHelper's is actually just a regular old method. – gcl1 Apr 12 '13 at 22:03
  • 8
    Thanks for this. It's a bug in the documentation (they never mention needing this or in IabHelper. IabHelper's launchPurchaseFlow() invokes the startIntentSenderForResult - which will call your activity's onActivityResult() when it's finished. – Jerry Brady Jan 05 '14 at 20:00
  • 2
    BTW, if you have a payment fragment like I do, where you do all of the callbacks in the fragment's Java, this solution works. Just create a public fragment object and use that to refer to the iabhelper, i.e. paymentFragment.mHelper.HandleActivity... – Anna Billstrom Apr 10 '14 at 05:08
  • 3
    I thought I had solved my problem when I found this... then I realized I had already implemented this function and it still doesn't work... *sigh* – Matt K Jun 13 '14 at 13:50
  • I want to consume the purchase i ihave made. But i am never get call back to the onPurchase finished. How can i make Asynconsume from onActivityResult – Rakesh Gondaliya Jul 17 '14 at 08:05
  • 3
    Note that overriding onActivityResult() on a Fragment doesn't work, it must be done on an Activity – cprcrack Oct 07 '14 at 14:58
  • 1
    @cprcrack Correct, the activityResult is called on the owning activity. This solution, combined with http://stackoverflow.com/questions/14131171/calling-startintentsenderforresult-from-fragment-android-billing-v3 worked for me to get around that. – Justin Smith Feb 21 '15 at 12:53
  • Thanks so much but honestly can you please tell us how the heck you got to that solution? – user2161301 Aug 08 '15 at 15:20
  • It is documented but at wrong place. IabHelper's handleActivityResult() method JavaDoc is having mention about it. It says... * Handles an activity result that's part of the purchase flow in in-app billing. If you are calling {@link #launchPurchaseFlow}, then you must call this method from your Activity's {@link android.app.Activity@onActivityResult} method. This method MUST be called from the UI thread of the Activity. – Pioneer May 24 '17 at 16:09
8

i just found out another important thing: the requestCode that is used to launch the purchase flow has to be >= 0!

i used "new Random().nextInt()" to generate a random requestCode, and sometimes it worked, sometimes it didn't. now i found out in the following documentation, that the requestCode should not be a negative number:

http://developer.android.com/reference/android/app/Activity.html#startActivityForResult%28android.content.Intent,%20int%29

drwuro
  • 81
  • 1
  • 1
4

I had the same issue and the onActivityResult was not called either.
Inspired from @Ghulam's answer I realized that the activity onActivityResult doesn't call the fragment's onActivityResult automatically so I had to do it manually.

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(mCurrentFragment!= null){
            mCurrentFragment.onActivityResult(requestCode, resultCode, data);
        }
    }
  • In my case, this answer worked, but I had to add an OnActivityResult override in my main activity ( https://github.com/onepf/OpenIAB/issues/166 ) – PayToPwn Nov 12 '18 at 22:25
1

You need to call protected void onActivityResult(); In your parent Activity instead of MainActivity(Trivial Drive) where from you are calling your MainActivity that is Trivial Drive Activity.

you will receive resultcode values -1 if purchase successful otherwise 0.

0

I was facing the same issue and the accepted solution was already implemented but couldn't tell what was causing this. Moving to the new Google Play Billing Library 1.0 (released on 19 September 2017) fixed the issue for me!

I hope following links will help someone who would like to implement the new library:

Google's blog post about the release

Google's youtube video

Play Billing Library Training Class

Google's Trivial Drive v2 Sample

Play Billing Library codelab published during Google I/O 2017

Play Billing Library Docs

Official reference for classes and methods

Releases notes

Mohit Singh
  • 1,452
  • 1
  • 18
  • 27