Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception: Can't start async operation (refresh inventory) because another async operation(launchPurchaseFlow) is in progress

Yes, there are two common answers to this problem:

One: A hack where one calls billingHelper.flagEndAsync(); before starting an async operation. This is generally considered as not recommended, and doesn't solve the problem in many cases.

Two: Calling the IabHelper's handleActivityResultmethod in the Activity's onActivityResult. The problem with this solution (besides me not understanding the purpose of this method) is that in my app the in-app billing operations are done in the app's Application class, because there are a bunch of activities in the application where a user may trigger an in-app purchase, and multiple possible entry points where there app needs to query the in-app purchase inventory. So I tried putting:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (!MyApplication.myAppInstance.mHelper.handleActivityResult(requestCode, resultCode, data))    
    super.onActivityResult(requestCode, resultCode, data);
}

into every activity where a user might start a purchase, but this has not solved the crashes. I don't really understand the purpose of this onActivityResult stuff, so I probably misunderstood how it's supposed to be implemented. Why does Google want to force me to do in-app billing operations from an activity anyway?

like image 734
TimSim Avatar asked Oct 13 '14 21:10

TimSim


2 Answers

I had a similar problem. Unfortunately the IabHelper code is not great... it's mostly good, but the threading model is kind of messy, which means it can become confusing how to properly handle the edge cases.

An easy way to get rid of the crashes is to simply wrap the method call with an if-statement check like:

if (!mHelper.isAsyncInProgress()) {
    mHelper.launchPurchaseFlow(...);
}

A better solution would be to rewrite the IabHelper code from scratch... but in this particular case the exception happens so rarely that it's probably not worth it. Unfortunately there isn't an easy fix in this case... the IabHelper sample code isn't the greatest and most of us will just decide to live with it.

like image 51
Alex Lockwood Avatar answered Nov 17 '22 18:11

Alex Lockwood


Having a flag to launchPurchaseFlow ignores multiple launches of purchase flow. But unfortunately, this fails if user clicks on Back Button by mistake from Google In App Billing serviceIntent. Flag should be reset to false in this case. Otherwise, user will not be able to try to purchase again.

if(!mHelper.isAsyncInProgress())
{
    mHelper.launchPurchaseFlow(...);
}

// Invoke below method to reset onBackPressed
public static void resetAsyncInProgress() {
    mHelper.setAsyncInProgress(false);
}
like image 36
FindIt Avatar answered Nov 17 '22 20:11

FindIt