3

From Google doc, it mentions

When you are done with this object, don't forget to call endConnection() to ensure proper cleanup. This object holds a binding to the in-app billing service and the manager to handle broadcast events, which will leak unless you dispose it correctly. If you created the object inside the onCreate(Bundle) method, then the recommended place to dispose is the onDestroy() method.

In my App, it will check purchase state from Google in loading page(LoadingActivity), then if user wants to purchase more products, he has to go to shopping page(ShoppingActivity).

As above, I should add BillingClient.endConnection() in LoadingActivity and ShoppingActivity. Every time go to shopping page, it is necessary to recall BillingClient.startConnection(this); call BillingClient.endConnection() when leaving the shopping page.

Joanne Chang
  • 75
  • 1
  • 9

1 Answers1

4

It should be called before applications's scope is destroyed. This is the Google's sample project. To be more specific this is the BillingLifeCycleClass. This is the recommended way of using billing client, by attaching a LifeCylceObserver to it.

Ahmed Al Hafoudh
  • 8,281
  • 1
  • 18
  • 34
Nadeem Shaikh
  • 1,160
  • 9
  • 17
  • 4
    This doesn't appear to be strictly correct. In Google's ClassyTaxiJava (and/or Kotlin) Sample project, the `BillingClientLifecycle` class is a `LifecycleObserver` which is bound to the lifecycle of the `MainActivity`, not the `Application` scope. So `endConnection()` is called on `MainActivity#onDestroy()`. – dugsmith Nov 03 '21 at 22:58
  • Agree that the connection does not have to be application level scoped but rather having it on the activity also works fine, just make sure that the disconnection and retry logic is in place. – Bawender Yandra Jun 10 '23 at 03:56