简体   繁体   English

android onActivityResult 没有在 Fragment 中被调用

[英]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我想用这个示例在应用程序计费中实现。我在片段中实现它。一切正常。但是当返回结果时,我应该调用 onactivityresult。我在片段中使用 onactivityresult 来访问 aibHelper 来管理它,但 onactivityresult 永远不会我应该怎么做在片段中调用 onactivityresult

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.我认为您是在 IabHelper 类中调用 startActivityForResult 。 You should call Fragment.startActivityForResult not Activity.startActivityForResult.您应该调用 Fragment.startActivityForResult 而不是 Activity.startActivityForResult。 To do that in class IabHelper you must implement two methods one with activity param and one with fragment param要在 IabHelper 类中执行此操作,您必须实现两种方法,一种使用活动参数,另一种使用片段参数

This usually happens when the parent activity implements onActivityResult() and completely consumes it without returning it.这通常发生在父 Activity 实现onActivityResult()并完全消耗它而不返回它时。 In the fragment's activity make sure you return onActivityResult whenenver you implement it.在片段的活动中,确保在实现它时返回 onActivityResult。 for example.例如。 Inside your activity, you can implement onActivityResult as follows:在您的活动中,您可以按如下方式实现 onActivityResult:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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