Более конкретно, Фрагмент A файла:
Код: Выделить всё
class FragmentA : Fragment(), MyAdapterClickListener, FragmentB.CallBListener {
override fun testFunFromFragmentB(myVarA: String) { someFun(myVarA) }
[...]
// This is how I setup the navigation to FragmentB
override fun MyAdapterClick(myVarB: Parcel) {
val action = FragmentDirections.actionFragmentAToFragmentB(myVarB)
Navigation.findNavController(requireView()).navigate(action)
}
Код: Выделить всё
class FragmentB : Fragment() {
interface CallBListener {
fun testFunFromFragmentB(myVarA: String)
}
private var myListener: CallBListener? = null
override fun onAttach(context: Context) {
super.onAttach(context)
myListener = findListener()
if (myListener == null) {
throw RuntimeException("$context must implement CallBListener")
}
}
private fun findListener(): CallBListener? {
val fragmentManager = parentFragmentManager
// Traverse the fragment stack to find the fragment that implements CallBListener
for (fragment in fragmentManager.fragments) {
if (fragment is CallBListener) {
return fragment
}
}
// Return null if no fragment implements CallBListener
return null
}
Как мне вызвать someFun
Как мне вызвать someFun код> FragmentA из FragmentB?
Подробнее здесь: https://stackoverflow.com/questions/790 ... s-listener
Мобильная версия