Java question regaring onActivityResult override

Monkey Targets Forums/Android/Java question regaring onActivityResult override

Xaron(Posted 2013) [#1]
Hi all,

I have a question to any Java professional here.

My IAP module for Android is almost finished but there is a tiny problem. The purchase callback isn't called and the solution is to override the onActivityResult method like it's explained here:

http://stackoverflow.com/questions/14800286/oniabpurchasefinished-never-called?rq=1

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);

    // Pass on the activity result to the helper for handling
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
        // not handled, so handle it ourselves (here's where you'd
        // perform any handling of activity results not related to in-app
        // billing...
        super.onActivityResult(requestCode, resultCode, data);
    }
    else {
        Log.d(TAG, "onActivityResult handled by IABUtil.");
    }
}


So far so good, but my IAPWrapper isn't an activity and using the BBAndroidGame.AndroidGame().GetActivity() call does not make it because it's obviously not "overridden" in the base (super) class called AndroidGame or MonkeyGame.

  public void purchase( String itemId, String payload )
  {
    _isPurchaseFinished = false;
    Activity activity = BBAndroidGame.AndroidGame().GetActivity();
    _helper.launchPurchaseFlow( activity, itemId, 10001, _purchaseFinishedListener, payload );
  }


Using the lines above the purchase itself DOES work but the callback is never called:
  // Callback for when a purchase is finished
  IabHelper.OnIabPurchaseFinishedListener _purchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener()
  {
    public void onIabPurchaseFinished( IabResult result, Purchase purchase )
    {
      _lastResult = result;
      if( result.isSuccess() )
        _isPurchaseFinished = true;
      else
        _isPurchaseFinished = false;
    }
  };


Of course I could alter the monkeytarget.java file but I don't like that. Another "solution" might be to extend my IAPWrapper class from AndroidGame (or MonkeyGame) BUT that doesn't work either because there is no running instance of my class as still the MonkeyGame instance is running.

All in all I'd like to avoid to change the monkeytarget.java file (which doesn't know anything about my wrapper class)...

Any ideas?


FDL(Posted 2013) [#2]
I've got the same issue. Any suggestions? It's a slightly urgent topic as the whole publishing of our app is pending on the google billing services being finished.


AdamRedwoods(Posted 2013) [#3]
you could try to create an anonymous class that calls a new activity that calls the IAP, but you'll still need to modify the manifest.xml for the second activity.

i suggest to put pressure on BRL to add this in the AndroidGame Activity, and calls _game (i think) with the proper overridden method in BBAndroidGame. That way it can be handled and used for any purpose (like Camera).

I'll see if i can do a pull on Monkey GitHub.


Xaron(Posted 2013) [#4]
I've solved that. You can take a look into my in app purchase module.

Basically what I did was to create a custom user target.


AdamRedwoods(Posted 2013) [#5]
ok cool.

i am still going to push for a better solution. i am currently testing one that may be best for multiple intent results (camera & IAP together). we'll see what i can do in a few hours.


Xaron(Posted 2013) [#6]
That would be nice! :) Biggest issue is still that I had to alter trans to let it NOT delete the src directory over and over again...


AdamRedwoods(Posted 2013) [#7]
ok, i've added the pull request. i was able to get the android camera working with monkey.


MikeHart(Posted 2013) [#8]
If Mark adds your pull request, does this mean that every Android app now the camera garbadge added to its package?


marksibly(Posted 2013) [#9]
> does this mean that every Android app now the camera garbadge added to its package?

No, it just adds a 'hook' for modules to use.

I've actually been playing around with this a bit today.

IMO, the best approach is to add something like this to androidgame.java:

class ActivityDelegate{
    public void OnActivityResult( int requestCode,int resultCode,Intent data ){
    }
    public void OnPause(){
    }
    public void OnRestart(){
    }
...etc...
}

class BBAndroidGame extends BBGame ...etc...{
...etc...
     public void AddActivityDelegate( ActivityDelegate delegate )...
...etc...
}


Reasoning here:

https://github.com/blitz-research/monkey/pull/26


MikeHart(Posted 2013) [#10]
Good to know.