When to call BillingClient.endConnection()?

2.2k Views Asked by At

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.

1

There are 1 best solutions below

3
On BEST ANSWER

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.