I have FragmentA and FragmentB. I am trying to update the values of FragmentA from FragmentB
I have an interface:
interface FragmentCallback {
fun onDataSent(sendUpdatedData: String, position: Int?)
}
In FragmentA, I override the interface function. I also instantiate FragmentB and call setFragmentCallback function
class FragmentA: Fragment(), FragmentCallback {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
FragmentB().setFragmentCallback(this)
}
//This function is not being called from FragmentB...
override fun onDataSent(sendUpdatedData: String, position: Int?) {
updateRecyclerView(sendUpdatedData, position!!)
}
In FragmentB (which is on top of FragmentA) I instantiate the FragmentCallback interface. When I am done editing and pop the fragment(backstack), I call the overridden function from FragmentA(onDataSent).
class FragmentB: Fragment(){
private var fragmentCallback: FragmentCallback? = null
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
btn_save.setOnClickListener{
fragmentCallback?.onDataSent(et_new_text.text.toString(), position )
fragmentManager?.popBackStack()
}
}
fun setFragmentCallback(callback: FragmentCallback?) {
fragmentCallback = callback
}
}
For some reason, when I pop FragmentB and call onDataSent
, through the line:
fragmentCallback?.onDataSent(et_new_text.text.toString(), position)
onDataSent will actually NOT be called.