简体   繁体   中英

OnActivityResult not getting called in Fragment from Activity

My flow of activity is like this.

In Fragment A, it allow user to capture image, and the image will be displayed on Activity B for user to do some editing.

Fragment 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")
        }
    }

In Activity B,once the done button is clicked, the edited image suppose to be return to Fragment A, where I expect " It get result from Activity B " will be displayed, but nothing get displayed !

Activity B

doneBtn.setOnClickListener {
            image.buildDrawingCache()
            val bitmap = image.getDrawingCache()
            val resultIntent = Intent()
            resultIntent.putExtra("bitmap", bitmap)
            setResult(Activity.RESULT_OK, resultIntent)
            finish()
        }

As one of the simplest options pass it through a static variable

In the Fragment A create a static field

    companion object {
        public var globalBitmap: Bitmap? = null
    }

and in Fragment A in onActivityResult method save bitmap to static variable like that

...
    if (requestCode == CAMERA_CAPTURE && resultCode == Activity.RESULT_OK) {
                val extras = data?.extras
                if (extras != null) {
                    globalBitmap = extras.get("data") as Bitmap

                    startActivityForResult(intent, 12)
                }
            }
...

In Activity B read result bitmap from Fragment A static variablelike that

FragmentA.globalBitmap

See i tried it a quick and added a breakpoint, and received callback in fragment.

在此处输入图片说明 use context

 val builder = android.support.v7.app.AlertDialog.Builder(context)

instead of activity

 val builder = android.support.v7.app.AlertDialog.Builder(activity)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM