How to run fun of the fragment from the main activity?

I have a TabLayout, tab to each is attached a fragment with its layout.
Tell me how to run the function in the fragment from the main activity?
In this fragment the main activity start like this:
(activity as MainActivity).myFun()

Kotlin
April 7th 20 at 15:19
1 answer
April 7th 20 at 15:21
Solution
FragmentManager.findFragmentByTag/ById

val fragment: Second_Fragment? =
supportFragmentManager.findFragmentById(R. id.constraintLayout2) as Second_Fragment?
fragment?.fromMainActivity()


the app crashes somehow - Guido_Zboncak commented on April 7th 20 at 15:24
@Mateo, well, it falls not "reason". It falls for quite a specific reason, which writes to the log. - Jaquelin_Moore commented on April 7th 20 at 15:27
Thank you, solved the problem. I am a newbie. The ID was incorrectly specified it turns out that together determine where the pasted text indicated from the fragment. Used this code:
(supportFragmentManager.findFragmentById(R. id.view_pager) as Second_Fragment).fromMainActivity() - Guido_Zboncak commented on April 7th 20 at 15:30

Find more questions by tags Kotlin