I tried to find a method in the new Navigation components but I didn\'t find anything about that.
I have the current destination with :
mainHostFrag
Using Michal's answer I wrote this extension function for testing:
@Suppress("UNCHECKED_CAST")
fun AppCompatActivity.getFragment(fragmentClass: Class): F? {
val navHostFragment = this.supportFragmentManager.fragments.first() as NavHostFragment
navHostFragment.childFragmentManager.fragments.forEach {
if (fragmentClass.isAssignableFrom(it.javaClass)) {
return it as F
}
}
return null
}
Used like this:
val myFragment = activity.getFragment(MyFragment::class.java)