1

code in fragment

 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);
            }
        }
    }

code in activity

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

Problem:

I have implemented BrainTree payment gateway with onBraintreeSubmit() method I startActivityResult and catch it onActivityResult but it's not being called in the fragment.

onActivityResult only gets called if I cancel payment but never when payment success or exception thrown

Community
  • 1
  • 1
  • Check It out https://stackoverflow.com/a/39267399/5773037 This is worked for me – Nikunj Paradva Aug 01 '17 at 07:07
  • 4
    Possible duplicate of [onActivityResult is not being called in Fragment](https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment) – aya salama Nov 05 '17 at 10:51

2 Answers2

3

First you have to call from activity Add below code in your activity

 @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);

}
Shweta Chauhan
  • 6,739
  • 6
  • 37
  • 57
0

If you call startActivityForResult() with activity instance, then onActivityResult will get in your activity not in fragment. This is the reason for your issue.

Modified the code as below. This will solve your issue.

public void onBraintreeSubmit(String clienTtoken, BottomMenu context) {
    DropInRequest dropInRequest = new DropInRequest().clientToken(clienTtoken);
    startActivityForResult(dropInRequest.getIntent(context.getContext()), BRAINTREE_REQUEST_CODE);
}
EKN
  • 1,886
  • 1
  • 16
  • 29
  • i tried like this fragment.startActivityForResult(dropInRequest.getIntent(getActivity()), BRAINTREE_REQUEST_CODE); but same problem –  Aug 01 '17 at 07:20
  • That's why i said to change the code like i mentioned above. If you want to get the onActivityResult() in your fragment, then you just need to call startActivityForResult() with out any class's instance. – EKN Aug 01 '17 at 07:23
  • i want to call in fragment see edited question? can u reply? –  Aug 01 '17 at 07:45
  • You want to call startActivity() from your fragment and want to get onAcivityResult() in the same fragment. This is your requirement, is it correct? – EKN Aug 01 '17 at 08:14
  • yes and i have changed codes to do so and can u see the codes edit and tell me what i m doing wrong? –  Aug 01 '17 at 08:18
  • Remove "fragment." from the startActivityResult() statement, that i have modified in your question – EKN Aug 01 '17 at 08:19
  • removed but still not getting called –  Aug 01 '17 at 08:24
  • Make sure you called setResult(RESULT_OK) in both Payment cancel and payment success condition – EKN Aug 01 '17 at 08:32