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); } } 
  • Perhaps the onActivityResult is called in the activation, not in the fragment, and therefore the problem. See if there is an overload of the BillingProcessor#purchase() method BillingProcessor#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 ♦
  • Tell me please, where can I see it?) Thank you - Wolfak
  • Well .. Someone is using a debugger, or you can simply output something to the logs in the necessary methods and see what and where will be displayed - YuriySPb ♦

1 answer 1

Problem solved. I already tried to solve it in this way, but I inserted this code in the wrong place. It is necessary that the Activity was mainly the onActivityResult method, which passed data to the onActivityResult of the fragment. The onActivityResult code from the main Activity:

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); for (Fragment fragment : getSupportFragmentManager().getFragments()) { if (fragment != null) { fragment.onActivityResult(requestCode, resultCode, data); } } }