1

Google's in app billing example com.example.android.trivialdrivesample.MainActivity contains the following code:

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

    // 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.");
    }
}

This seems somewhat dangerous, because onActivityResult will not behave as expected until after mHelper has been initialised. It would seem better to drop if (mHelper == null) return in favour of if (mHelper == null || !mHelper.handleActivityResult(requestCode, resultCode, data)), i.e.,

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

Is the original code really dangerous? Or have I misunderstood?

user2768
  • 794
  • 8
  • 31

2 Answers2

1

Hey I'm also working on InApp Purchase since 10 days and I've successfully integrated in my existing app and ready to make it live. Initially when i had started doing this I've downloaded google InApp Billing Example called "Trivial Drive" from here.

But it didn't help me much as it has lots of issues and bugs, So I've decided do it on my own from scratch using new v3 api which you can find here. This tutorial has clear explanation that would help you and also if you have time, see this youtube video where google employee had explained clearly how to integrate it.

Also if you want quick example, I've a sample app which you can download from here.

The following video also explains how to integrate InApp Purchase. Please go through it.

https://www.youtube.com/watch?v=-h2ESH71hAI

Ramesh
  • 1,252
  • 3
  • 12
  • 30
0

I totally agree. i think it's much better to do something like this:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        if (requestCode == REQUEST_CODE_RECOVER_PLAY_SERVICES) {
            if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this, "Google Play Services must be installed.", Toast.LENGTH_SHORT).show();
                finish();
            }
        } else if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            // LOAD FROM CAMERA
            ivf.LoadImage(tmpFilePath, quality, resolution, format);

        } else if (requestCode == BUY_PREMIUM && resultCode == Activity.RESULT_OK) {

            this.onPremiumPurchase();

        } else if( requestCode == RC_REQUEST) {

            if (mHelper == null) {
                Toast.makeText(mContext, "mHelper is NULL", Toast.LENGTH_LONG).show();
                return;
            }
            // Pass on the activity result to the helper for handling
            mHelper.handleActivityResult(requestCode, resultCode, data);
        }

    }
L.Grillo
  • 960
  • 3
  • 12
  • 26