onIabPurchaseFinished never called.

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!");
             }


    }
};

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.");
    }
}

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


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);
        }
    }

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.