I have an app that has many activities. One of the activity is for showing purchase options.
In the sample apps for the billing library (https://github.com/googlesamples/android-play-billing), BillingClientLifecycle and BillingManager are used, both of which are associated to a single activity, so the connection is opened/closed when the activity is created/destroyed.
However in an app with many activities, it seems not ideal to do this separately for different activities. I also want to check on app start whether the subscriptions are valid.
I am thinking of creating the BillingClient in the app's Application subclass. However if I do this, I would only be opening the BillingClient connection and not closing it (as there's no onDestroy method in there). Has anyone done this before and hit any issues? Also is this against best practices and will it cause any issues with network / performance?
It looks like this can be done with architecture components. I.e. in your application's OnCreate, call:
ProcessLifecycleOwner.get().lifecycle.addObserver(billingClient)
And just inject the billingClient into the activities that need it.