Issue
I am new to android development and while working with intents, I have a payment intent and a payment listener and I am trying to fire another activity or intent when the listener picks up a payment response. The issue is, I am unable to get context int listener class implementation.
The class that I am trying to create a new intent in is:
public class PaymentConnectorListener implements IPaymentConnectorListener {
@Override
public void onSaleResponse(SaleResponse response) {
String result;
if (response.getSuccess()) {
result = "Sale was successful";
} else {
result = "Sale was unsuccessful" + response.getReason() + ":" + response.getMessage();
}
}
}
How I initialize the listener class is:
final IPaymentConnectorListener ccListener = new PaymentConnectorListener();
and where I use the listener is:
paymentConnector = new PaymentConnector(this, account, ccListener, remoteApplicationId);
paymentConnector.initializeConnection();
I want to fire an intent on payment response but the onSaleResponse() method does not have context. Also, switching from angular, can you explain a bit more of what context is.
Solution
you can modify your code to include the Context parameter in the constructor
public class PaymentConnectorListener implements IPaymentConnectorListener {
private Context context;
public PaymentConnectorListener(Context context) {
this.context = context;
}
@Override
public void onSaleResponse(SaleResponse response) {
String result;
if (response.getSuccess()) {
result = "Sale was successful";
} else {
result = "Sale was unsuccessful" + response.getReason() + ":" + response.getMessage();
}
// Use the context to start a new activity or create a new intent
Intent intent = new Intent(context, YourActivity.class);
intent.putExtra("result", result);
context.startActivity(intent);
}
}
When initializing the PaymentConnectorListener, pass the appropriate Context object to its constructor
final IPaymentConnectorListener ccListener = new PaymentConnectorListener(this);
Make sure that this refers to a valid Context object within the scope where you are initializing the PaymentConnectorListener
Answered By - zaid khan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.