片段中的代码
private static final int BRAINTREE_REQUEST_CODE = 777;
BottomMenu fragment = this;
public void onBraintreeSubmit(String clienTtoken) {
DropInRequest dropInRequest = new DropInRequest().clientToken(clienTtoken);
startActivityForResult(dropInRequest.getIntent(getActivity()), BRAINTREE_REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BRAINTREE_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
Toast.makeText(getActivity(), result.getPaymentMethodNonce().getNonce() + "", Toast.LENGTH_SHORT).show();
// use the result to update your UI and send the payment method nonce to your server
sendPaymentNonceToServer(result.getPaymentMethodNonce().getNonce());
} else if (resultCode == Activity.RESULT_CANCELED) {
// the user canceled
} else {
// handle errors here, an exception may be available in
Exception error = (Exception) data.getSerializableExtra(DropInActivity.EXTRA_ERROR);
}
}
}
活动代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
问题:
我已经使用onBraintreeSubmit()方法实现了BrainTree支付网关我使用startActivityResult并将其捕获到onActivityResult但是它没有在片段中被调用。
onActivityResult仅在我取消付款时被调用,但从未在付款成功或抛出异常时被调用
答案 0 :(得分:3)
首先你需要从活动中打电话 在您的活动中添加以下代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameLayout);
fragment.onActivityResult(requestCode, resultCode, data);
}
答案 1 :(得分:0)
如果使用活动实例调用startActivityForResult(),则onActivityResult将进入您的活动而不是片段。这就是你问题的原因。
修改代码如下。这将解决您的问题。
public void onBraintreeSubmit(String clienTtoken, BottomMenu context) {
DropInRequest dropInRequest = new DropInRequest().clientToken(clienTtoken);
startActivityForResult(dropInRequest.getIntent(context.getContext()), BRAINTREE_REQUEST_CODE);
}