I can not figure out a few days. How to implement getting the result of Android In-app billing in the fragment? When making a test payment (after a successful payment for the purchase), nothing happens the first time, when you click a second time (and subsequent ones) on the buy button, the action from the onProductPurchased method is successful. Why the action after a successful purchase is not performed the first time and how to solve it? All operations with billing occur inside the fragment. I read a lot of information on the Internet and similar questions, but did not understand how to solve this problem. I hope for your help.
My code is:
// ΠΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΡ ΠΈ ΠΊΠ½ΠΎΠΏΠΊΠ° Π² onCreateView bp = new BillingProcessor(context, "ΠΠΎΠΉ ΠΊΠ»ΡΡ", this); bp.initialize(); // ΠΠ»ΠΈΠΊ ΠΏΠΎ ΠΊΠ½ΠΎΠΏΠΊΠ΅ Π΄Π»Ρ ΠΎΠΏΠ»Π°ΡΡ PayButt.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { // ΠΡΠΏΠΎΠ»Π½ΡΠ΅ΠΌ ΠΎΠΏΠ»Π°ΡΡ bp.purchase((Activity) context, ProductID); } } ); // ΠΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΡ ΠΈ ΠΊΠ½ΠΎΠΏΠΊΠ° Π² onCreateView // Π‘ΡΠ°Π½Π΄Π°ΡΡΠ½ΡΠ΅ ΠΌΠ΅ΡΠΎΠ΄Ρ // ΠΠΏΠ»Π°ΡΠ° @Override public void onBillingInitialized() { /* * ΠΡΠ·ΡΠ²Π°Π΅ΡΡΡ, ΠΊΠΎΠ³Π΄Π° ΠΎΠ±ΡΠ΅ΠΊΡ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·ΠΈΡΠΎΠ²Π°Π½ ΠΈ ΠΌΠΎΠΆΠ½ΠΎ ΡΠΎΠ²Π΅ΡΡΠ°ΡΡ ΠΏΠΎΠΊΡΠΏΠΊΠΈ */ } @Override public void onProductPurchased(@NonNull String productId, TransactionDetails details) { /* * ΠΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΠΊΠΎΠ³Π΄Π° ΠΏΠΎΠΊΡΠΏΠΊΠ° ΡΠΎΠ²Π΅ΡΡΠ΅Π½Π° */ if (bp.isPurchased(productId)) { Toast toastTrue = Toast.makeText(context, "ΠΠΏΠ»Π°ΡΠ° Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π° ΡΡΠΏΠ΅ΡΠ½ΠΎ!", Toast.LENGTH_SHORT); toastTrue.show(); // ΠΠ΅ΡΠ΅Ρ
ΠΎΠ΄ΠΈΠΌ Π½Π° Π³Π»Π°Π²Π½ΡΡ Intent intent = new Intent(getActivity(), MainController.class); startActivity(intent); } } @Override public void onBillingError(int errorCode, Throwable error) { /* * ΠΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΠΏΡΠΈ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΠΎΠ²Π΅Π½ΠΈΠΈ ΠΊΠ°ΠΊΠΈΡ
-Π»ΠΈΠ±ΠΎ ΠΎΡΠΈΠ±ΠΎΠΊ ΠΏΡΠΈ ΡΠΎΠ²Π΅ΡΡΠ΅Π½ΠΈΠΈ ΠΏΠΎΠΊΡΠΏΠΊΠΈ */ } @Override public void onPurchaseHistoryRestored() { /* * ΠΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΠΎΠ΄ΠΈΠ½ ΡΠ°Π· ΠΏΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠΌ Π·Π°ΠΏΡΡΠΊΠ΅ ΠΏΠΎΡΠ»Π΅ ΡΡΡΠ°Π½ΠΎΠ²ΠΊΠΈ ΠΈΠ»ΠΈ ΠΏΠ΅ΡΠ΅ΡΡΡΠ°Π½ΠΎΠ²ΠΊΠΈ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡ. * Π‘Π»ΡΠΆΠΈΡ Π΄Π»Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΠΏΡΠΈΠΎΠ±ΡΠ΅ΡΡΠ½Π½ΡΡ
ΡΠ°Π½Π΅Π΅ ΠΏΠΎΠΊΡΠΏΠΎΠΊ, Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ, Π½Π° Π΄ΡΡΠ³ΠΎΠΌ ΡΡΡΡΠΎΠΉΡΡΠ²Π΅ */ } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (!bp.handleActivityResult(requestCode, resultCode, data)) { super.onActivityResult(requestCode, resultCode, data); } }
BillingProcessor#purchase()methodBillingProcessor#purchase()accepts the fragment. Then the necessary method will be called and it should work. Otherwise, you will have to somehow call the call from the activation to the fragment. - Yuriy SPb β¦