Skip to content Skip to sidebar Skip to footer

Onpurchasestatechange Not Getting Called

The sample code for the in-app feature has: onPurchaseStateChange(PurchaseState purchaseState, String itemId, int quantity, long purchaseTime, String developerPayload)

Solution 1:

I'm not sure what the problem is and why sometimes the @overrided onPurchaseStateChange in the requesting Activity isn't called but I can verify that onPurchaseStateChange in the BillingReceiver and BillingService are always called.

For some reason, sometimes, the callback just doesn't arrive all the way back to the calling Activity.

By the way, this issue seems to only happen when you purchase a managed product or a subscription. If you buy an unmanaged product, the callback will always arrive.

A solution might be (in the requesting purchase Activity - Dungeons Activity in the sample application):

@OverridepublicvoidonPurchaseStateChange(PurchaseState purchaseState, String itemId, int quantity, long purchaseTime, String developerPayload) {

            if (purchaseState == PurchaseState.PURCHASED) {

                onPurchaseStateChange = true;

                // Do whatever you need to do after purchase here first.

            }
        }

        @OverridepublicvoidonRequestPurchaseResponse(RequestPurchase request, ResponseCode responseCode) {


            if (responseCode == ResponseCode.RESULT_OK) {

                if (!onPurchaseStateChange) {
                    // If onPurchaseStateChange = false, the onPurchaseStateChange callback didn't arrive from the BillingService, so you can perform your after purchase actions here.
                }

            } elseif (responseCode == ResponseCode.RESULT_USER_CANCELED) {

            } else {

            }
        }

Solution #2 (seems to be the right solution): Google's sample code includes this method:

@OverrideprotectedvoidonStop() {
        super.onStop();

        ResponseHandler.unregister(YOUR_PURCHASE_OBSERVER);
    }

This causes the observer to be unregistered the second you open the Play store purchase activity. When you go back to your activity (whether after purchase or back button), the onStart is called and re-starts the purchaseObserver but not always with the purchase information - which causes the onPurchaseStateChange to never be called.

A solution will be to remove the unregister from onStop and move it to onDestroy. Another solution is to leave it there but start it with a queue that hold previously received details. See here: Android in-app-billing. When to unregister the ResponseHandler?

Post a Comment for "Onpurchasestatechange Not Getting Called"