Calling startIntentSenderForResult from Fragment (Android Billing v3)

前端 未结 11 1161
广开言路
广开言路 2020-11-29 02:46

The new Android Billing v3 documentation and helper code uses startIntentSenderForResult() when launching a purchase flow. I want to start a purchase flow (and

11条回答
  •  再見小時候
    2020-11-29 03:27

    I suggest two solutions:

    1.) Put the IabHelper mHelper on the activity and call the IabHelper from the fragment.

    Something like:

    To use this solution, Declare IabHelper as public in the activity and use a method to call the launcher from the Fragment.

    public class MyActivity extends Activity{
    
        public IabHelper mHelper
    
        public purchaseLauncher(){
    
        mHelper.launchPurchaseFlow(this, SKU_GAS, 10001,   
             mPurchaseFinishedListener, "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
    
       }
    
        /*The finished, query and consume listeners should also be implemented in here*/
    }
    
    public class FragmentActivity extends Fragment{
    
          MyActivity myAct = (MyActivity) getActivity();
    
          myAct.purchaseLauncher();
    
    }
    

    2.) In onActivityResult, call the appropriate fragment that contains the IabHelper object. Appropriate fragment can have an access method to the helper object.

    protected void onActivityResult(int requestCode, int resultCode,Intent data)    
    {
        super.onActivityResult(requestCode, resultCode, data);
    
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentByTag("YourTag");       
        if (fragment != null)
        {
            ((MyFragmentWithIabHelper)fragment).onActivityResult(requestCode, resultCode,data);
        } 
    }
    

提交回复
热议问题