1.在当前Activity中的跳转方法用:
startActivityForResult<ControlModelActivity>(10)
//10是requestCode
2.在目标Activity中向当前Activity回传:
val intent : Intent = Intent()
var firPosition: Int = 0
var senPosition: Int = 0
var thrPosition: Int = 0
var levelTotalName: String = ""
requireActivity().intent.putExtra("LevelTotalName", levelTotalName)
requireActivity().intent.putExtra("LevelOnePosition", firPosition)
requireActivity().intent.putExtra("LevelTwoPosition", senPosition)
requireActivity().intent.putExtra("LevelThreePosition", thrPosition)
requireActivity().setResult(11, requireActivity().intent)
// resultCode为结果码,,传的值可用于在当前接收Activity中做判断,但常为OK/CANCEL
3.在当前Activity中接收、处理数据:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) { //判断返回码
when (requestCode) { //判断请求码
10 -> {
data?.let {
numOrder = it.getIntExtra("numCommand", -2)
binding.controlMove.showMove(numOrder)
}
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
}
}
// requestCode : 请求码,定义到类里。为任一数值。
// resultCode:返回码:常为 OK / CANCEL 用来表明返回成功/失败