apologies if this is trivial - I am new to Kotlin and coroutines (read Elizarov etc.)
I have an Activity that, in its onCreate(), needs to get a price from the Play Store. I wrote this:
...
lifecycleScope.launch {
val pd = StoreHelper.getProductDetails(pp!!)
val price = pd?.oneTimePurchaseOfferDetails?.formattedPrice
if (price != null) {
buyNowBtn.text = getString(R.string.buy_price, price)
} else {
buyNowBtn.text = getString(R.string.buy_no_price)
}
}
...
Then, the helper's getProductDetails() I copy-pasted from the docs (notice suspend):
suspend fun getProductDetails(PitchProductId: Long) : ProductDetails? {
var pd: ProductDetails? = null
val queryProductDetailsParams =
QueryProductDetailsParams.newBuilder()
...
.build()
// leverage queryProductDetails Kotlin extension function
val productDetailsResult = withContext(Dispatchers.IO) {
billingClient.queryProductDetails(queryProductDetailsParams)
}
if (productDetailsResult.productDetailsList?.isNotEmpty() == true) {
pd = productDetailsResult.productDetailsList!![0]
}
return pd
}
The problem: The Activity can request prices for different products. The first call works. But all subsequent calls don't: I get back a null. I bump the app and get the same thing again (first call works). Looks like I need to cancel something... Sorry, totally lost. Thank you!