I need help with Android Integration inside a fragment , It is working fine when the code is implemented in an Activity but in My Case , i need to implement it in a Fragment .
I am trying to call the dropin ui from a Fragment but unable to do so.
Steps i followed :- 1) I fetched the braintree token using get_braintree_token(rootview.getContext()) .
2) After getting token i passed it to onBraintreeSubmit(braintreeToken ,viewb) 3) When it reaches startActivityForResult() in onBraintreeSubmit function ,The BrainTree UI is not inflated .It directly moves to onActivityResult() with resultcode 1 and some random Request code . I have written my functions Below.
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.fragment_map, container, false);
get_braintree_token(rootview.getContext());
return rootview;
}
public void get_braintree_token(final Context viewb)
{
StringRequest stringRequest = new StringRequest(Request.Method.GET, "My server URL", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
braintreeToken = response.toString();
Log.e("Braintreetoken","token is:"+braintreeToken);
onBraintreeSubmit(braintreeToken, viewb);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
braintreeToken = null;
}
});
// Add the request to the RequestQueue.
requestQueue.add(stringRequest);
}
public void onBraintreeSubmit(String token, Context c) {
Log.e("tokenreceived",token);
DropInRequest dropInRequest = new DropInRequest();
dropInRequest.clientToken(token);
Intent drop = dropInRequest.getIntent(c);
startActivityForResult(drop, BRAINTREE_REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BRAINTREE_REQUEST_CODE) {
if (resultCode == 1) {
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
} 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);
}
}
}