0

I have a bottomNavigationActivity, and a few fragments (e.g. homeFragment, settingsFragment).

In homeFragment, I have a recycler view. When selected a row, it will request for a client token and it will display a braintree dropin UI,

In homeRecyclerAdapter:

view.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){

...

clientToken = response.body().getClientToken();

DropInRequest dropInRequest = new DropInRequest()
        .clientToken(clientToken);

// Display Drop in
Context context = view.getContext();
Intent intent = dropInRequest.getIntent(context);
context.startActivity(intent);

}

It's able to display the braintree drop in, however, once it's dismiss or a payment method is selected, how do I get the event?

I've managed to get the event if I'm using a activity to display this drop in instead of using a recycler adapter, this is achieved using "startActivityForResult(dropInRequest.getIntent(getBaseContext()), 10)" and "onActivityResult":

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 10) { 
            if (resultCode == Activity.RESULT_OK) {
                DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
                // use the result to update your UI and send the payment method nonce to your server

                // Run postNonceToServer code
                Log.d(TAG, "RESULT_OK");

            } else if (resultCode == Activity.RESULT_CANCELED) {
                // the user canceled

                Log.d(TAG, "RESULT_CANCELED");

            } else {
                // handle errors here, an exception may be available in
                Exception error = (Exception) data.getSerializableExtra(DropInActivity.EXTRA_ERROR);

                Log.d(TAG, "EXTRA_ERROR");

            }
        }
    }
user1872384
  • 6,886
  • 11
  • 61
  • 103
  • you can't get `onActivityResult` called with `startActivity` – Blackbelt Nov 29 '18 at 09:33
  • 1
    use `startActivityForResult` – Rohit5k2 Nov 29 '18 at 09:34
  • 1
    Related [How to manage `startActivityForResult` on Android?](https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android) – AskNilesh Nov 29 '18 at 09:34
  • 2
    You really shouldn't be starting an `Activity` directly from your `Adapter`. Callback to the `Fragment` somehow – e.g., via `interface` – where you can use the `Fragment`'s `startActivityForResult()`. – Mike M. Nov 29 '18 at 09:37
  • 1
    Thanks @MikeM. Created an interface and managed to call startActivityForResult and get the result in onActivityResult :D You're a life saver lol... Sometimes I wonder why on android we have fragment and activities, compared to iOS where we only have viewController which makes it easier – user1872384 Nov 29 '18 at 10:42

0 Answers0