gpt4 book ai didi

kotlin - 如何在 Kotlin 的一行中声明 startActivityforResult()?

转载 作者:行者123 更新时间:2023-12-04 17:52:20 25 4
gpt4 key购买 nike

我想调用startActivityforResult在 Kotlin 的一行语句中。像这样的东西:

startActivityForResult(Intent(this@MainActivity2,MainActivity::class.java),1))

最佳答案

这是对 startActivityForResult 进行单行调用的一种简洁方法。在结果事件中,您必须创建一个伴随对象来设置启动意图。

事件

class MyActivity {
companion object {
const val CONTACT_REQUEST = 1
}

fun activityCall() {
startActivityForResult(MyActivity.getLaunchIntent(this), CONTACT_REQUEST)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Check which request we're responding to
if (requestCode == MyActivity.CARD_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == Activity.RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.

// Do something with the contact here (bigger example below)
}
}
}
}

结果类
class MyResultActivity {
companion object {
fun getLaunchIntent(context: Context): Intent {
val intent = Intent(context, MyActivity::class.java)
return intent
}
}
}

关于kotlin - 如何在 Kotlin 的一行中声明 startActivityforResult()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50617598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com