-2

In adapter class, user can capture image when holder.image is clicked. I have set onActivityResult in Fragment, but it not getting called.

Fragment

 grid.setAdapter(ImageListAdapter(getActivity(), listOfImagesPath))
     ....

  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        val mAdapter: ImageListAdapter?=null
        mAdapter?.onActivityResult(requestCode, resultCode, data);
        longToast("abc")
        var bitmap: Bitmap? = null
        if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
            longToast("hello")
        } else {
            longToast("bye")
        }
    }

Adapter

 class ImageListAdapter(
        private val context: FragmentActivity?,
        private val imgPics: List<String>?
    ) : BaseAdapter() {

        override fun getItem(position: Int): Any {
            return position
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getCount(): Int {
            if (imgPics != null)
                return imgPics.size
            else
                return 1;
        }


        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

            var holder: ItemHolder
            var convertView = convertView

            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.grid_item, null)
                holder = ItemHolder()
                holder.image = convertView!!.findViewById(R.id.imageView)
                holder.image?.setImageResource(R.drawable.ic_add)
                holder.image?.setColorFilter(R.color.colorGainsboro, PorterDuff.Mode.SRC_ATOP);

                convertView.tag = holder

            }
            holder = convertView?.tag as ItemHolder
                  ....
            holder.image?.setImageBitmap(bm)
                }
            }

            holder.image?.setOnClickListener{
                dispatchTakePictureIntent()
            }
            return convertView
        }

        private fun dispatchTakePictureIntent() {
            try {
                val captureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                context?.startActivityForResult(captureIntent, 1);
            } catch (e: ActivityNotFoundException) {
                e.printStackTrace()
            }
        }

        fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
            Log.e("MyAdapter", "onActivityResult")
        }

        internal class ItemHolder {
            var image: ImageView? = null
        }
    }

Does anyone has an idea?

Tony
  • 2,515
  • 14
  • 38
  • 71
  • Which activity type you're using as base, AppCompatActivity? – sinek Feb 14 '19 at 14:46
  • Try to use host activity instead of fragment. From its onActivityResult invoke function of your adapter – Artem Botnev Feb 14 '19 at 14:46
  • @sinek Fragment() – Tony Feb 14 '19 at 14:47
  • @ArtemBotnev sorry,dont get it – Tony Feb 14 '19 at 14:48
  • Make sure to use extend your activity from AppCompatActivity and call super.onActivityResult if you ever override it. – sinek Feb 14 '19 at 14:49
  • @sinek This is my Fragment class `class CreateFragment : BaseFragment(true)` – Tony Feb 14 '19 at 14:51
  • and BaseFragment `abstract class BaseFragment(var showBottomNavigation: Boolean) : Fragment() {` – Tony Feb 14 '19 at 14:51
  • 1
    Isn't that the same question as https://stackoverflow.com/questions/54685955/onactivityresult-not-getting-called-in-fragment-where-intent-pass-from-adapter-c that you just asked earlier ? – shkschneider Feb 14 '19 at 14:55
  • @shkschneider Do you know what is the issue ? – Tony Feb 14 '19 at 14:59
  • 1
    Possible duplicate of [OnActivityResult not getting called in Fragment where intent pass from adapter class](https://stackoverflow.com/questions/54685955/onactivityresult-not-getting-called-in-fragment-where-intent-pass-from-adapter-c) – shkschneider Feb 14 '19 at 15:04
  • @shkschneider added `super.onActivityResult` but still no luck :) – Tony Feb 14 '19 at 15:08

2 Answers2

4

I think the problem is that you call startActivityForResult on your activity's context.

 private val context: FragmentActivity?,
 context?.startActivityForResult(captureIntent, 1);

You should use instead your Fragment's context if you want to get results there..

 private val fragment: Fragment?,
 fragment?.startActivityForResult(captureIntent, 1);

So you should pass your fragment reference to the adapter.

sinek
  • 2,458
  • 3
  • 33
  • 55
  • If i pass Fragment, I will get error on this line `grid.setAdapter(ImageListAdapter(getActivity(), listOfImagesPath))` – Tony Feb 14 '19 at 14:58
  • @jguerinet required fragment found fragmentActivity – Tony Feb 14 '19 at 15:00
1

@sinek is right, you need to call startActivityForResult() from the Fragment instance instead of the activity. This requires passing in the Fragment instance to your adapter and fixing the old context calls as a result:

class ImageListAdapter(
    private val fragment: Fragment,
    private val imgPics: List<String>?
) : BaseAdapter() {

    ...

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var holder: ItemHolder
        var convertView = convertView

        if (convertView == null) {
            // Here I am simply using the parent Context, as parent will never be null
            //   (Notice how I changed ViewGroup? to ViewGroup above)
            //   But if you prefer you can use fragment.context
            convertView = LayoutInflater.from(parent.context).inflate(R.layout.grid_item, null)
            ...
        }

        ...

    }

    private fun dispatchTakePictureIntent() {
        try {
            val captureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            fragment.startActivityForResult(captureIntent, 1);
        } catch (e: ActivityNotFoundException) {
            e.printStackTrace()
        }
    }

    ...
}

Then, in your Fragment:

grid.setAdapter(ImageListAdapter(this, listOfImagesPath))
jguerinet
  • 1,429
  • 1
  • 14
  • 21