| 
23 | 23 | import java.util.ArrayList;  | 
24 | 24 | import java.util.Arrays;  | 
25 | 25 | import java.util.List;  | 
 | 26 | +import java.lang.ref.WeakReference;  | 
26 | 27 | import org.apache.cordova.CallbackContext;  | 
27 | 28 | import org.apache.cordova.CordovaInterface;  | 
28 | 29 | import org.apache.cordova.CordovaPlugin;  | 
 | 
35 | 36 | public class Iap extends CordovaPlugin {  | 
36 | 37 | 
 
  | 
37 | 38 |   private BillingClient billingClient;  | 
38 |  | -  private Context context;  | 
39 |  | -  private Activity activity;  | 
 | 39 | +  private WeakReference<Context> contextRef;  | 
 | 40 | +  private WeakReference<Activity> activityRef;  | 
40 | 41 |   private CallbackContext purchaseUpdated;  | 
41 | 42 | 
 
  | 
42 | 43 |   public void initialize(CordovaInterface cordova, CordovaWebView webView) {  | 
43 | 44 |     super.initialize(cordova, webView);  | 
44 |  | -    context = cordova.getContext();  | 
45 |  | -    activity = cordova.getActivity();  | 
 | 45 | +    contextRef = new WeakReference<>(cordova.getContext());  | 
 | 46 | +    activityRef = new WeakReference<>(cordova.getActivity());  | 
46 | 47 |     billingClient = getBillingClient();  | 
47 | 48 |   }  | 
48 | 49 | 
 
  | 
@@ -103,7 +104,7 @@ public void run() {  | 
103 | 104 | 
 
  | 
104 | 105 |   private BillingClient getBillingClient() {  | 
105 | 106 |     return BillingClient  | 
106 |  | -      .newBuilder(this.context)  | 
 | 107 | +      .newBuilder(this.contextRef.get())  | 
107 | 108 |       .enablePendingPurchases()  | 
108 | 109 |       .setListener(  | 
109 | 110 |         new PurchasesUpdatedListener() {  | 
@@ -248,7 +249,7 @@ private void purchase(String json, CallbackContext callbackContext) {  | 
248 | 249 |     try {  | 
249 | 250 |       SkuDetails skuDetails = new SkuDetails(json);  | 
250 | 251 |       BillingResult result = billingClient.launchBillingFlow(  | 
251 |  | -        activity,  | 
 | 252 | +        activityRef.get(),  | 
252 | 253 |         BillingFlowParams.newBuilder().setSkuDetails(skuDetails).build()  | 
253 | 254 |       );  | 
254 | 255 |       int responseCode = result.getResponseCode();  | 
 | 
0 commit comments