我希望在 Kotlin 中开启一个结果 Activity 。
我已经阅读了文章 Can't Call StartActivityForResult in Anko
所以我将 AppCompatActivity 转换为 Activity
但是代码 (mContext as Activity).startActivityForResult<UIAddEditBackup>(requestCode = 1, "id" to 2L)
还没有运行,为什么?
我的代码
class UIMain : AppCompatActivity() {
private lateinit var mContext: Context
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_main)
mContext = this
btnExit.setOnClickListener {
startActivity<UIAddEditBackup>("id" to 2L) //OK
(mContext as Activity).startActivityForResult<UIAddEditBackup>(requestCode = 1, "id" to 2L) //Error
}
}
}
最佳答案
根据 Kotlin Offical Doc on named arguments ,
您正在使用 startActivityForResult<UIAddEditBackup>(requestCode = 1, "id" to 2L)
。情况就是这样。
所以,像这样修复:
startActivityForResult<UIAddEditBackup>(1, "id" to 2L)
或者startActivityForResult<UIAddEditBackup>(requestCode = 1, params = *arrayOf("id" to 2L))
(Anko startActivityForResult 的原型(prototype)是 inline fun <reified T: Activity> Activity.startActivityForResult(requestCode: Int, vararg params: Pair<String, Any?>)
)。引用 ( the same page above )
在这种情况下,
(mContext as Activity).
不是必需的。关于android - 为什么我不能在 Kotlin 中使用 startActivityForResult?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49604887/