活动内适配器的startActivityForResult,onActivityResult

时间:2019-09-12 16:21:01

标签: android kotlin

我有一个适配器,我强制转换了MainActivity传递的上下文,这里是Adapter代码

class Adapter {
....
....
inner class ViewHolder (view: View) : RecyclerView.ViewHolder(view) {
    init {
       val intent = Intent((context as MainActivity), FooActivity::class.java)
       context.startActivityForResult(intent, FROM_FOO_ACTIVITY)
    }
}

然后在FooActivity

val intent = Intent()
setResult(Activity.RESULT_OK, intent)
finish()

然后在MainActivity

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && requestCode == FROM_FOO_ACTIVITY) {
            //do my thing
        }
    }

问题是没有到达onActivityResult,我的方法有什么问题?

0 个答案:

没有答案