Calling startIntentSenderForResult from Fragment (

2020-01-27 02:12发布

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

For example the documentation suggests calling

startIntentSenderForResult(pendingIntent.getIntentSender(),
    1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
    Integer.valueOf(0));

and the helper code calls

mHelper.launchPurchaseFlow(this, SKU_GAS, 10001,   
    mPurchaseFinishedListener, "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");

which calls startIntentSenderForResult().

The problem is, calling startIntentSenderForResult() causes onActivityResult() to be called on the parent Activity rather than on the Fragment that it was called from (where the IabHelper resides).

I could receive the onActivityResult() in the parent Activity and then manually call the onActivityResult() on the Fragment, but is there a way to make a call to startIntentSenderForResult() from a Fragment that returns the result directly to that Fragment's onActivityResult()?

11条回答
唯我独甜
2楼-- · 2020-01-27 03:04

I suggest creating some sort of generic handling of this issue in your base activity class if you have access to it.

For example:

public abstract class BaseActivity extends Activity {
    private List<ActivityResultHandler> mResultHandlers 
        = new ArrayList<ActivityResultHandler>();

    public void registerActivityResultHandler(ActivityResultHandler resultHandler) {
        mResultHandlers.add(resultHandler);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        for (ActivityResultHandler resultHandler : mResultHandlers) {
            resultHandler.handle();
        }
    }
}

Of course, you'll need to implement ActivityResultHandler interface by your fragments and register them on activity startup.

查看更多
beautiful°
3楼-- · 2020-01-27 03:06
if (requestCode == RC_REQUEST) 
{
    Intent intent = new Intent(ContainerAvtivity.this,ContainerAvtivity.class);
    startActivity(intent);
    finish();
}

RC_REQUEST is same as you used to launch purchase flow

Add this in the onActivityResult of your Activity.The inventory listener will produce the desired result for you.(I know its a temp fix but worked for me)).

查看更多
Root(大扎)
4楼-- · 2020-01-27 03:07

You need to call

super.onActivityResult(requestCode, resultCode, data);

at the beginning of your Activity's and Fragment's onActivityResult to cascade the Results to the fragments.

In my FragmentActivity this reads as

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // No action here, call super to delegate to Fragments
    super.onActivityResult(requestCode, resultCode, data);
}
查看更多
女痞
5楼-- · 2020-01-27 03:08

You need to pass fragment and data to parent activity, then call fragment onActivityResult from the parent activity.

like this

in fragment:

HomeActivity activity = (HomeActivity) getActivity();
activity.purchaseLauncher(this, mHelper, productDTO.getSku(), RC_REQUEST, mPurchaseFinishedListener, PAYLOAD);

in parent activity:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (storeFragment != null) {
            storeFragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    public void purchaseLauncher(StoreFragment storeFragment, IabHelper mHelper, String sku, int requestCode, IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener, String payload) {
        this.storeFragment = storeFragment;
        mHelper.launchPurchaseFlow(this, sku, requestCode, mPurchaseFinishedListener, payload);
    }

查看更多
贪生不怕死
6楼-- · 2020-01-27 03:11

1) You should modify your resultCode (RC_REQUEST) to put fragment index to it.

int rc_reqest = RC_REQUEST +  ((getActivity().getSupportFragmentManager().getFragments().indexOf(this)+1)<<16)  ;      
mHelper.launchPurchaseFlow(getActivity(), sku, rc_reqest ,mPurchaseFinishedListener, payload);

2) in IabHelper.launchPurchaseFlow(...)

change mRequestCode = requestCode

to

mRequestCode = requestCode&0xffff;
查看更多
登录 后发表回答