OnActivityResult not getting called in Fragment where intent pass from adapter class
Asked Answered
V

2

-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 !

Vilayet answered 14/2, 2019 at 8:24 Comment(11)
Downvoters should explain why !Vilayet
check this #6148384Anguish
@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)Vilayet
https://mcmap.net/q/411508/-onactivityresult-not-called-inside-fragment-duplicate and this one https://mcmap.net/q/411509/-how-can-i-call-onactivityresult-inside-fragment-and-how-it-workAnguish
@NileshRathod my startActivityForResult need to call in Adapter classVilayet
Why don't you move your dispatchTakePictureIntent() inside your activity and call startActivityForResult from fragmentAnguish
Let us continue this discussion in chat.Vilayet
Possible duplicate of onActivityResult is not being called in FragmentAnse
@Anse added super.onActivityResult but still no luckVilayet
Your problem is about the context and who calls startActivityForResult (which tells who will receive the result). It is explained in #6148384 and others. You just opened two duplicated questions IMO.Anse
"added super.onActivityResult but still no luck" make sure to read the entire answer. First part was about super, the second part was not.Anse
I
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)
}
Isobelisocheim answered 14/2, 2019 at 9:13 Comment(4)
thanks for your effort but it doesnt help. I have found a solution. Can you delete your answer so I can delete my post?Vilayet
@Vilayet post your solution, it may help othersIsobelisocheim
Is it impossible to call startActivityForResult from adapter class and onActivityResult in fragment ???Vilayet
@Vilayet you can pass fragment (this) instead of context. and then call fragment.startActivityForResult(..)Isobelisocheim
T
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.

Tireless answered 14/2, 2019 at 8:44 Comment(1)
I want to call startActivityForResult in adapter class, so I would need context as ActivityVilayet
I
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)
}
Isobelisocheim answered 14/2, 2019 at 9:13 Comment(4)
thanks for your effort but it doesnt help. I have found a solution. Can you delete your answer so I can delete my post?Vilayet
@Vilayet post your solution, it may help othersIsobelisocheim
Is it impossible to call startActivityForResult from adapter class and onActivityResult in fragment ???Vilayet
@Vilayet you can pass fragment (this) instead of context. and then call fragment.startActivityForResult(..)Isobelisocheim

© 2022 - 2024 — McMap. All rights reserved.