这是我的startActivityForResult()
:
val initialPosition = frame.tag as Int
val currentPosition = constraint.indexOfChild(frame)
Log.d(TAG, "initial position: $initialPosition") // prints initial position: 2
Log.d(TAG, "current position: $currentPosition") // prints current position: 2
val galleryIntent = Intent().apply {
type = "image/*"
action = Intent.ACTION_PICK
putExtra("initialPosition", initialPosition)
putExtra("currentPosition", currentPosition)
setResult(5)
}
startActivityForResult(Intent.createChooser(galleryIntent, "Pick image"), PICK_IMAGE_REQUEST_CODE)
但是,我无法在此处检索数据:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK){
Log.d(TAG, "ResultCode: $resultCode SUCCESS") // prints -1 SUCCESS
}
when (requestCode){
PICK_IMAGE_REQUEST_CODE -> {
Log.d(TAG, "${data?.getIntExtra("initialPosition", 0)}") // prints 0
Log.d(TAG, "${data?.extras?.getInt("initialPosition")}") // prints 0
Log.d(TAG, "${data?.extras?.getInt("currentPosition")}") // prints 0
}
}
super.onActivityResult(requestCode, resultCode, data)
}
知道为什么吗?
答案 0 :(得分:0)
setResult()
不会设置galleryIntent
的结果代码。可以设定结果的是您在Intent.ACTION_PICK
期间选择的活动。
假设您选择了系统映像选择器。如果选择图像,它将返回RESULT_OK
(为-1),如果您按下图像,则返回RESULT_CANCELED
(为0)。
请注意,如果您使用的是resultCode
,那么您将无法更改Implicit Intent
返回的其他活动。