1

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()
        }
Tony
  • 2,515
  • 14
  • 38
  • 71
  • Try to remove the line from all places intent.putExtra ("bitmap", bitmap) and see the logs longToast ("It get the result from Activity B"). If log message "It get the result from Activity B" will appear then the problem is that the bitmap parameter is too large for Bundle. Or Try to log onActivityResult in your host fragment activity. – SergeyBukarev Feb 27 '19 at 08:07
  • @SergeyBukarev you're right, I get the Toast now, but how to pass bitmap? – Tony Feb 27 '19 at 08:12
  • Pass it through a static variable or save bitmap to a file and pass file name from bundle. Bundle parameter has a size limit. – SergeyBukarev Feb 27 '19 at 08:19
  • @SergeyBukarev let me try – Tony Feb 27 '19 at 08:22
  • Possible duplicate of [onActivityResult is not being called in Fragment](https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment) – Sergio Feb 27 '19 at 08:45

2 Answers2

1

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
SergeyBukarev
  • 1,478
  • 1
  • 12
  • 19
0

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

enter image description hereuse context

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

instead of activity

 val builder = android.support.v7.app.AlertDialog.Builder(activity)
Sandip Savaliya
  • 784
  • 6
  • 19
  • can you explain how your answer helped ? – Tony Feb 27 '19 at 08:47
  • You are passing an activity as a reference of alertdialog, so dialog will run on FragmentActivity. and any result requested will be dispatched to the activity instead of where you requested from. using context is more friendly to startActivityForResult. it dispatches result to calling context. – Sandip Savaliya Feb 27 '19 at 08:47
  • `OnActivityResult` not getting called is because the parameter bitmap is too large. – Tony Feb 27 '19 at 08:59
  • Have you tried captured an image and display it on next Activity, then call onActivityResult again ? – Tony Feb 27 '19 at 09:06
  • i think you are doing it in a wrong way. First, capture image, and once you get an URI back in onActivityResult, try opening it in another activity to display if you want so. otherwise, camera intent will already display and request to accept or deny for using captured image. – Sandip Savaliya Feb 27 '19 at 09:16
  • ya, the reason is because the bitmap parameter – Tony Feb 27 '19 at 13:34