-3

So in my adapter class, I would like to allow user to capture image

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

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

I want the onActivityResult in a fragment class get called, but it doesn't.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        val imageListAdapter : ImageListAdapter?=null
        imageListAdapter?.onActivityResult(requestCode, resultCode,data)
                if (requestCode == 1 && resultCode == Activity.RESULT_OK) 
                {
                    longToast("called")
                }else{
                    longToast("no")
                }
            }

There are no toast displayed. How to solve ?

I realize the onActivityResult works if I put in one of my Activity class, but I want to put at Fragment class !

Tony
  • 2,515
  • 14
  • 38
  • 71
  • Downvoters should explain why ! – Tony Feb 14 '19 at 08:56
  • check this https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment – AskNilesh Feb 14 '19 at 11:20
  • @NileshRathod if I put the onActivityResulty in one of the activity class, it works. But I want it call in fragment ! Is it because of this line ? `(context as Activity)` – Tony Feb 14 '19 at 11:24
  • https://stackoverflow.com/a/49704422/7666442 and this one https://stackoverflow.com/a/44622657/7666442 – AskNilesh Feb 14 '19 at 11:26
  • @NileshRathod my `startActivityForResult` need to call in Adapter class – Tony Feb 14 '19 at 11:29
  • Why don't you move your `dispatchTakePictureIntent()` inside your activity and call `startActivityForResult` from fragment – AskNilesh Feb 14 '19 at 11:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188414/discussion-between-tony-and-nilesh-rathod). – Tony Feb 14 '19 at 11:36
  • 1
    Possible duplicate of [onActivityResult is not being called in Fragment](https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment) – shkschneider Feb 14 '19 at 15:06
  • @shkschneider added super.onActivityResult but still no luck – Tony Feb 14 '19 at 15:08
  • Your problem is about the context and who calls startActivityForResult (which tells who will receive the result). It is explained in https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment and others. You just opened two duplicated questions IMO. – shkschneider Feb 14 '19 at 15:13
  • "added super.onActivityResult but still no luck" make sure to read the entire answer. First part was about super, the second part was not. – shkschneider Feb 14 '19 at 15:14

2 Answers2

1

If you want fragment's onActivityResult() to be called, call startActivityForResult(intent, id) from fragment, not from activity (try pass fragment reference to adapter).

Also, make sure you haven't overridden activity's onActivityResult() or call super.onActivityResult() in activity.

  • I want to call startActivityForResult in adapter class, so I would need `context as Activity` – Tony Feb 14 '19 at 08:48
0

as nikita said you should call startActivityForResult from fragment if you want to get results in fragment.

I want to call startActivityForResult in adapter class, so I would need context as Activity Then create OnItemClick interface make your fragment implement it and pass it in a constructor to your Adapter. Then when a user clicks on an item call interface method

interface OnItemClickListener{
   onClick(item:T)
}

Foo: Fragment, OnItemClickListener{
...
    onClick(item:T){
        startActivityForResult...
    }
...
    initAdapter(){
        Adapter(listener=this,...)
    }

}

And in your adapter

...
itemView.setOnClickListener{
    listener.onClick(item)
}
svkaka
  • 3,942
  • 2
  • 31
  • 55
  • thanks for your effort but it doesnt help. I have found a solution. Can you delete your answer so I can delete my post? – Tony Feb 14 '19 at 09:21
  • 1
    @Tony post your solution, it may help others – svkaka Feb 14 '19 at 09:24
  • Is it impossible to call startActivityForResult from adapter class and onActivityResult in fragment ??? – Tony Feb 14 '19 at 10:08
  • @Tony you can pass fragment (this) instead of context. and then call fragment.startActivityForResult(..) – svkaka Feb 14 '19 at 12:47