Home > database >  Android - Get current fragment
Android - Get current fragment

Time:11-03

This question has been ask a lot, but i have not seen a recent answer.

This in contained in my main activity :

<androidx.fragment.app.FragmentContainerView
    android:id="@ id/fragment_container_view"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:navGraph="@navigation/navigation_map" />

I have tried

fragment manager deprecated

-> The fragment manager is deprecated

SupportFragmentManager

-> The support fragment manager does not work

I would like to know what is the fragment loaded in my FragmentContainerView

CodePudding user response:

I'll use the supportFragmentManager as suggested

The findFragmentById was not working for it though, but this seen in another linked question goes well :

navHostFragment = supportFragmentManager.fragments.first()

And the the usual

wantedFragment = navHostFragment.getChildFragmentManager().getFragments().first()
  • Related