I am having a problem with an AlertDialog that I am using to initiate an In-App Purchase. When the user taps the "Buy" button, the In-App Purchase fires as expected, but the AlertDialog does not close. Then, when the In-App Pur开发者_如何学编程chase finishes, the program returns to my app but the AlertDialog is still open.
If I comment out the buyCard() function, the AlertDialog will close. Any idea why the AlertDialog is not closing when the In-App Billing is involved?
final CharSequence[] items = {"Buy","Close"};
AlertDialog.Builder builder = new AlertDialog.Builder(Card.this);
builder.setTitle("Want to Buy?");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if(item == 0) { // BUY THE ITEM
dialog.dismiss();
buyCard();
} else if (item == 1) { // Don't Buy
dialog.dismiss();
}
}
});
AlertDialog alert = builder.create();
alert.show();
I'm sure this question has long been figured out, but the probable solution would be to move buyCard();
to above dialog.dismiss();
So, like this:
if(item == 0) { // BUY THE ITEM
buyCard();
dialog.dismiss();
} else if (item == 1) { // Don't Buy
dialog.dismiss();
}
Reason being, the dialog can't run the buyCard();
line since you've already dismissed it. Kind of like trying to run code after a return
statement.
精彩评论