Kotlin:使用 startActivityForResult 与同伴一起启动 activity
Kotlin: Launching activity with companion using startActivityForResult
我正在构建一个倒计时 activity,它需要将剩余时间传回调用 activity(当倒计时仅被部分使用时。)。
class CountDownActivity : AppCompatActivity() {
companion object{
const val REQUEST_START_COUNTDOWN: Int = 2
fun CountDownIntent(iToDoRecord: iToDoRecord): Intent {
return Intent(maincontext, CountDownActivity::class.java).apply {
putExtra(MainActivity.INTENT_COUNTDOWN_TASK, iToDoRecord.itaskname)
putExtra(MainActivity.INTENT_CALLING_DATE, MainActivity.currentDate)
putExtra(MainActivity.INTENT_COUNTDOWN_TIME, iToDoRecord.itaskduration.toLong())
putExtra(MainActivity.INTENT_COUNTDOWN_TASKID, iToDoRecord.itaskid)
putExtra(MainActivity.INTENT_COUNTDOWN_TASKMILLI, iToDoRecord.itaskmilliremaining.toLong())
}
}
}.....
}
然后我从我的 reclyclerview 中这样调用它:
val intentitd = Intent(holder.textViewID.getContext(), CountDownActivity.CountDownIntent(currentiToDo)::class.java)
//intentitd.putExtra(MainActivity.INTENT_CALLING_DATE, currentdate)
startActivityForResult(CountDownActivity,CountDownActivity.CountDownIntent(currentiToDo),CountDownActivity.REQUEST_START_COUNTDOWN)
编译器给我以下错误:
"Type mismatch: inferred type is CountDownActivity.Companion but Activity was expected"
我做错了什么?
您可以尝试将 activity 调用为:
val intent = CountDownActivity.CountDownIntent(currentiToDo)
startActivityForResult(intent,CountDownActivity.REQUEST_START_COUNTDOWN)
我正在构建一个倒计时 activity,它需要将剩余时间传回调用 activity(当倒计时仅被部分使用时。)。
class CountDownActivity : AppCompatActivity() {
companion object{
const val REQUEST_START_COUNTDOWN: Int = 2
fun CountDownIntent(iToDoRecord: iToDoRecord): Intent {
return Intent(maincontext, CountDownActivity::class.java).apply {
putExtra(MainActivity.INTENT_COUNTDOWN_TASK, iToDoRecord.itaskname)
putExtra(MainActivity.INTENT_CALLING_DATE, MainActivity.currentDate)
putExtra(MainActivity.INTENT_COUNTDOWN_TIME, iToDoRecord.itaskduration.toLong())
putExtra(MainActivity.INTENT_COUNTDOWN_TASKID, iToDoRecord.itaskid)
putExtra(MainActivity.INTENT_COUNTDOWN_TASKMILLI, iToDoRecord.itaskmilliremaining.toLong())
}
}
}.....
}
然后我从我的 reclyclerview 中这样调用它:
val intentitd = Intent(holder.textViewID.getContext(), CountDownActivity.CountDownIntent(currentiToDo)::class.java)
//intentitd.putExtra(MainActivity.INTENT_CALLING_DATE, currentdate)
startActivityForResult(CountDownActivity,CountDownActivity.CountDownIntent(currentiToDo),CountDownActivity.REQUEST_START_COUNTDOWN)
编译器给我以下错误: "Type mismatch: inferred type is CountDownActivity.Companion but Activity was expected"
我做错了什么?
您可以尝试将 activity 调用为:
val intent = CountDownActivity.CountDownIntent(currentiToDo)
startActivityForResult(intent,CountDownActivity.REQUEST_START_COUNTDOWN)