1

Trying to set up in app purchases in my app and it's in a fragment. Got it working in a activity but struggling to get it into a fragment. If anyone can see any error or can help me out would be great. It just goes into the activity no showing the in app purchase.

Code:

public class Fragment_11 extends Fragment implements OnClickListener {

private static final String TAG = "BillingService";

private Context mContext;

boolean mIsRegistered = false;

// this has already been set up for my app at the publisher's console
static final String IS_REGISTERED = "myregistered";

static final int RC_REQUEST = 10001;

 // The helper object
IabHelper mHelper; 


public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1,
        container, false);

String base64EncodedPublicKey = "[my public key]"; // (from publisher's console for my app)

// Create the helper, passing it our context and the public key to verify signatures with
Log.d(TAG, "Creating IAB helper.");
mHelper = new IabHelper(getActivity(), base64EncodedPublicKey);

// enable debug logging (for a production application, you should set this to false).
mHelper.enableDebugLogging(true);

// Start setup. This is asynchronous and the specified listener
// will be called once setup completes.
Log.d(TAG, "Starting setup.");
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
    public void onIabSetupFinished(IabResult result) {
        Log.d(TAG, "Setup finished.");

        if (!result.isSuccess()) {
            complain("Problem setting up in-app billing: " + result);
            return;
        }

        // Hooray, IAB is fully set up. Now, let's get an inventory of stuff we own.
        Log.d(TAG, "Setup successful. Querying inventory.");
        mHelper.queryInventoryAsync(mGotInventoryListener);
    }
});

   // Set the onClick listeners
  view.findViewById(R.id.button1).setOnClickListener(this);

 Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(getActivity(), player11.class);
        getActivity().startActivity(intent);
    }
});
  Button button2 = (Button) view.findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(getActivity(), player12.class);
            getActivity().startActivity(intent);
        }
    });
      Button button3 = (Button) view.findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(getActivity(), player13.class);
                getActivity().startActivity(intent);
            }
        });
          Button button4 = (Button) view.findViewById(R.id.button4);
            button4.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(getActivity(), player14.class);
                    getActivity().startActivity(intent);
                }
            });
              Button button5 = (Button) view.findViewById(R.id.button5);
                button5.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(getActivity(), player15.class);
                        getActivity().startActivity(intent);
                    }
                });

            return view;
            }

            // Listener that's called when we finish querying the items we own
            IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
                public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
                    Log.d(TAG, "Query inventory finished.");
                    if (result.isFailure()) {
                        complain("Failed to query inventory: " + result);
                        return;
                    }

                    Log.d(TAG, "Query inventory was successful.");

                    // Do we have the premium upgrade?
                    mIsRegistered = inventory.hasPurchase(IS_REGISTERED);
                    Log.d(TAG, "User is " + (mIsRegistered ? "REGISTERED" : "NOT REGISTERED"));

                    setWaitScreen(false);
                    Log.d(TAG, "Initial inventory query finished; enabling main UI.");
                }
            };      

            // User clicked the "Register" button.
            private void startRegistered() {
                Log.d(TAG, "Register button clicked; launching purchase flow for upgrade.");
                setWaitScreen(true);
                mHelper.launchPurchaseFlow(getActivity(), IS_REGISTERED, RC_REQUEST, mPurchaseFinishedListener);
            }

            @Override
            public 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.");
                }
            }

            // Callback for when a purchase is finished
            IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
                public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
                    Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
                    if (result.isFailure()) {
                        // Oh noes!
                        complain("Error purchasing: " + result);
                        setWaitScreen(false);
                        return;
                    }

                    Log.d(TAG, "Purchase successful.");

                    if (purchase.getSku().equals(IS_REGISTERED)) {
                        Log.d(TAG, "User has registered..");
                        alert("Thank you.");
                        mIsRegistered = true;
                        setWaitScreen(false);
                    }
                }
            };

            // We're being destroyed. It's important to dispose of the helper here!
            @Override
            public void onDestroy() {
                // very important:
                Log.d(TAG, "Destroying helper.");
                if (mHelper != null) mHelper.dispose();
                mHelper = null;
            }

            void complain(String message) {
                Log.e(TAG, "**** Register Error: " + message);
                alert("Error: " + message);
            }

            void setWaitScreen(boolean set) {
                // just a dummy for now
            }

            void alert(String message) {
                AlertDialog.Builder bld = new AlertDialog.Builder(getActivity());
                bld.setMessage(message);
                bld.setNeutralButton("OK", null);
                Log.d(TAG, "Showing alert dialog: " + message);
                bld.create().show();
            }
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                case R.id.button1:
                    startRegistered();
                    break;
                default:
                    break;
                }


  }}
user2407147
  • 1,508
  • 2
  • 22
  • 40
  • 1
    See the discussion [here](http://stackoverflow.com/questions/14131171/calling-startintentsenderforresult-from-fragment-android-billing-v3), which may help. – AutonomousApps Nov 26 '13 at 23:25

2 Answers2

1

Probably problem is in onActivityResult. I had problem in that. You need to override it in activity the same way you did in fragment. If you won't override onActivityResult it starts it's super class and your fragment will not start OnIabPurchaseFinishedListener.

Penzzz
  • 2,814
  • 17
  • 23
0

I had this problem. What I did was implement the purchase in the Main activity. So I sent which SKU to the main activity and process it there. When the purchase is finished I set a flag weather it succeeded or not. Then restart the fragment and check the flag in onresume function.

zizutg
  • 1,070
  • 14
  • 20