I am using the IabHelper
utility classes, as recommended by Google's tutorial, and I'm being hit hard by this error. Apparently IabHelper
can not run multiple async operations at the same time. I even managed to hit it by trying to start a purchase while the inventory taking was still in progress.
I have already tried to implement onActivityResult
in my main class as suggested here, but I don't even get a call to that method before the error hits. Then I found this but I have no idea where to find this flagEndAsync
method - it's not in the IabHelper
class.
Now I'm looking for a way around this (without reimplementing the whole she-bang). The only solution I can think of is to create a boolean field asyncActive
that is checked before an async task is started, and not do it if there is another task active. But that has many other problems, and doesn't work across activities. Also I'd prefer to have an async task queue up and run as soon as it's allowed to, instead of not running at all.
Any solutions for this issue?
Another major issue with the IabHelpr class is the poor choice of throwing RuntimeExcptions (IllegalStateException) in multiple methods. Throwing RuntimeExeptions from your own code in most cases is not desirable due to the fact that they are unchecked exceptions. That is like sabotaging your own application- if not caught, these exceptions will bubble up and crash your app.
The solution to this is to implement your own checked exception and change the IabHelper class to throw it, instead of the IllegalStateException. That will force you to handle this exception everywhere it could be thrown in your code at compile time.
Here is my custom exception:
Once we make the changes in the IabHelper class, we can handle our checked exception in our code where we call the class methods. For example:
My solution is simple
1.) Make the mAsyncInProgress variable visible outside of IabHelper
2.) Use this in your Activity like:
A simple trick that did it for me was to create a method in IabHelper:
and then in your code, just check:
A simple tricky solution
before calling purchaseItem method just add this line
so your code looks this way
Note: don't forget to make public flagEndAsync() method in IabHelper if you call it from another package.
I was having the same issue until I stumbled upon another SO thread. I'm including a touched up version of the code found in the other thread that you need to include in your Activity that initialises the purchase.
I had the same issue and the problem was that I didn't implement the method onActivityResult.