Recently I upgraded to Android BillingClient version 4.0.0 from 3.0.2. I am facing 2 issues in Async functions. The UI code written inside Async functions like showing AlertDialog and disabling button is not working. Showing AlertDialog inside billingClient.queryPurchaseHistoryAsync() is not working in billingclient version 4.0.0. This was working fine in version 3.0.2. Similarly disabling a button inside billingClient.queryPurchasesAsync() is not working in billingclient version 4.0.0.
The code is given below. Please guide me how to solve this problem. Thanks in advance.
public class SubscribeActivity extends AppCompatActivity implements
PurchasesUpdatedListener, BillingClientStateListener,
AcknowledgePurchaseResponseListener {
private Button btnSubscribe;
private BillingClient billingClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subscribe);
btnSubscribe = findViewById(R.id.btnSubscribe);
billingClient = BillingClient.newBuilder(this).setListener(this)
.enablePendingPurchases().build();
}
private void generatePurchaseDetails() {
billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS,
(billingResult, purchaseList) -> {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// some more code here...
builder.create().show();
// This AlertDialog is not getting displayed in billingclient version 4.0.0. This was working fine in version 3.0.2.
});
}
private void queryPurchases() {
billingClient.queryPurchasesAsync(BillingClient.SkuType.SUBS,
(billingResult, purchases) -> {
// The below line is not getting executed properly in billingclient 4.0.0.
btnSubscribe.setEnabled(false);
// some more code here...The control does not come to these lines of code.
});
}
}
I also faced the same issue and figured out that billingclient 4 is using background threads instead of UI threads on callbacks. So you have to run your code which updates UI in a UI thread.
private void queryPurchases() {
billingClient.queryPurchasesAsync(BillingClient.SkuType.SUBS,
(billingResult, purchases) -> {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// The below line is not getting executed properly in billingclient 4.0.0.
btnSubscribe.setEnabled(false);
// some more code here...The control does not come to these lines of code.
}
}
});
});
}