简体   繁体   中英

android onActivityResult is not being called in Fragment

I want to implement in app billing with this sample .I implement it in fragment.everything is ok.but when the result is returned I should call onactivityresult.I use onactivityresult in the fragment to access the aibHelper to manage it but onactivityresult will never be called.what should I do call onactivityresult in fragment

this is my codes:

class CartFragment : Fragment(), IabHelper.OnIabPurchaseFinishedListener,IabHelper.OnConsumeFinishedListener {

    lateinit var viewModel: CartViewModel
    lateinit var bazarViewModel: BazarViewModel
    lateinit var iabHelper: IabHelper
    var productId = ""
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        var view = inflater.inflate(R.layout.fragment_cart, container, false)
        var recycler = view.findViewById<RecyclerView>(R.id.rv_cart_list)
        recycler.layoutManager = LinearLayoutManager(context)
        iabHelper = IabHelper(context, resources.getString(R.string.rsa))
        iabHelper.startSetup {
            if (it.isSuccess) {
                Log.i("LOG", "setup finished")
                Toast.makeText(context,"ready",Toast.LENGTH_SHORT).show()
            }
        }
        viewModel = ViewModelProviders.of(this).get(CartViewModel::class.java)
        viewModel.getProduct().observe(this, Observer {
            recycler.adapter = CartAdapter(it) {
                productId = it
                iabHelper.launchPurchaseFlow(activity!!,it,1003,this)
            }
        })
        return view
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == 1003) {
            iabHelper.handleActivityResult(requestCode, resultCode, data)
            Log.i("LOG", "on activity if")
        } else {
            Log.i("LOG", "on activity else")
            super.onActivityResult(requestCode, resultCode, data)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        iabHelper.dispose()

    }
    override fun onIabPurchaseFinished(result: IabResult?, info: Purchase?) {
        Log.i("LOG", "on purchase finished")
        iabHelper.consumeAsync(info,this)
    }
    override fun onConsumeFinished(purchase: Purchase?, result: IabResult?) {
        Toast.makeText(context,"consume success",Toast.LENGTH_SHORT).show()
        Log.i("LOG","consume finished")

    }
}

I think you are calling startActivityForResult in IabHelper class. You should call Fragment.startActivityForResult not Activity.startActivityForResult. To do that in class IabHelper you must implement two methods one with activity param and one with fragment param

This usually happens when the parent activity implements onActivityResult() and completely consumes it without returning it. In the fragment's activity make sure you return onActivityResult whenenver you implement it. for example. Inside your activity, you can implement onActivityResult as follows:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == REQUEST_CODE) {
        //perform some actions
    } else {
        Log.i("LOG", "on activity else")
        //perform another actions
    }
  return super.onActivityResult(requestCode, resultCode, data) //this line is the most important
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM