OnActivityResult 未在 Activity 的 Fragment 中调用
OnActivityResult not getting called in Fragment from Activity
我的activity流程是这样的
在Fragment
A中,允许用户截取图像,图像将显示在Activity B
上,供用户进行一些编辑。
片段A
mImageListAdapter.mAddImageClickListener = object : ImageListAdapter.AddImageClickListener {
override fun addImageClicked() {
val options = arrayOf<CharSequence>("Take Photo", "Choose From Gallery", "Cancel")
val builder = android.support.v7.app.AlertDialog.Builder(activity)
builder.setTitle("Select Option")
builder.setItems(options) { dialog, item ->
if (options[item] == "Take Photo") {
dialog.dismiss()
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(intent, CAMERA_CAPTURE)
}
}
builder.show()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
var bitmap: Bitmap? = null
if (requestCode == CAMERA_CAPTURE && resultCode == Activity.RESULT_OK) {
val extras = data?.extras
if (extras != null) {
bitmap = extras.get("data") as Bitmap
val intent = Intent(activity, ActivityB::class.java)
intent.putExtra("bitmap", bitmap)
startActivityForResult(intent, 12)
}
} else if (requestCode == 12 && resultCode == Activity.RESULT_OK) {
longToast("It get result from Activity B")
} else {
longToast("Nothing")
}
}
在 Activity B 中,单击完成按钮后,编辑后的图像应该 return 到片段 A,我希望在其中显示“It get result from Activity B
”,但是未显示任何内容!
Activity B
doneBtn.setOnClickListener {
image.buildDrawingCache()
val bitmap = image.getDrawingCache()
val resultIntent = Intent()
resultIntent.putExtra("bitmap", bitmap)
setResult(Activity.RESULT_OK, resultIntent)
finish()
}
作为最简单的选项之一,通过静态变量传递它
在 Fragment A 中创建一个静态字段
companion object {
public var globalBitmap: Bitmap? = null
}
并在 onActivityResult
方法中的片段 A 中将位图保存到静态变量中
...
if (requestCode == CAMERA_CAPTURE && resultCode == Activity.RESULT_OK) {
val extras = data?.extras
if (extras != null) {
globalBitmap = extras.get("data") as Bitmap
startActivityForResult(intent, 12)
}
}
...
在ActivityB中从FragmentA中读取结果位图像这样的静态变量
FragmentA.globalBitmap
看到我快速尝试并添加了一个断点,并在片段中收到了回调。
使用上下文
val builder = android.support.v7.app.AlertDialog.Builder(context)
而不是activity
val builder = android.support.v7.app.AlertDialog.Builder(activity)
我的activity流程是这样的
在Fragment
A中,允许用户截取图像,图像将显示在Activity B
上,供用户进行一些编辑。
片段A
mImageListAdapter.mAddImageClickListener = object : ImageListAdapter.AddImageClickListener {
override fun addImageClicked() {
val options = arrayOf<CharSequence>("Take Photo", "Choose From Gallery", "Cancel")
val builder = android.support.v7.app.AlertDialog.Builder(activity)
builder.setTitle("Select Option")
builder.setItems(options) { dialog, item ->
if (options[item] == "Take Photo") {
dialog.dismiss()
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(intent, CAMERA_CAPTURE)
}
}
builder.show()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
var bitmap: Bitmap? = null
if (requestCode == CAMERA_CAPTURE && resultCode == Activity.RESULT_OK) {
val extras = data?.extras
if (extras != null) {
bitmap = extras.get("data") as Bitmap
val intent = Intent(activity, ActivityB::class.java)
intent.putExtra("bitmap", bitmap)
startActivityForResult(intent, 12)
}
} else if (requestCode == 12 && resultCode == Activity.RESULT_OK) {
longToast("It get result from Activity B")
} else {
longToast("Nothing")
}
}
在 Activity B 中,单击完成按钮后,编辑后的图像应该 return 到片段 A,我希望在其中显示“It get result from Activity B
”,但是未显示任何内容!
Activity B
doneBtn.setOnClickListener {
image.buildDrawingCache()
val bitmap = image.getDrawingCache()
val resultIntent = Intent()
resultIntent.putExtra("bitmap", bitmap)
setResult(Activity.RESULT_OK, resultIntent)
finish()
}
作为最简单的选项之一,通过静态变量传递它
在 Fragment A 中创建一个静态字段
companion object {
public var globalBitmap: Bitmap? = null
}
并在 onActivityResult
方法中的片段 A 中将位图保存到静态变量中
...
if (requestCode == CAMERA_CAPTURE && resultCode == Activity.RESULT_OK) {
val extras = data?.extras
if (extras != null) {
globalBitmap = extras.get("data") as Bitmap
startActivityForResult(intent, 12)
}
}
...
在ActivityB中从FragmentA中读取结果位图像这样的静态变量
FragmentA.globalBitmap
看到我快速尝试并添加了一个断点,并在片段中收到了回调。
val builder = android.support.v7.app.AlertDialog.Builder(context)
而不是activity
val builder = android.support.v7.app.AlertDialog.Builder(activity)