-1

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

Foxhunt
  • 764
  • 1
  • 9
  • 23
  • The Jetpack edition of `FragmentManager` is not deprecated. You appear to be using the framework `FragmentManager`, which *is* deprecated. "I would like to know what is the fragment loaded in my FragmentContainerView" -- call `findFragmentById()` on `FragmentManager`. – CommonsWare Oct 30 '21 at 23:19
  • 1
    Use `supportFragmentManager` instead – Zain Oct 30 '21 at 23:53
  • [This](https://stackoverflow.com/questions/51385067/android-navigation-architecture-component-get-current-visible-fragment) will help you get the current fragment using navigation components – Zain Oct 30 '21 at 23:55
  • 1
    This will help: https://stackoverflow.com/questions/50689206/how-i-can-retrieve-current-fragment-in-navhostfragment?noredirect=1&lq=1 – Nitin Prakash Oct 31 '21 at 04:39
  • The supportFragmentManager doesn't get me the fragment, maybe because i use a FragmentContainerView ? – Foxhunt Oct 31 '21 at 11:05
  • I found out using the `supportFragmentManager` as shown in the 3d answer of the @NitinPrakash linked question. Thanks everyone ! – Foxhunt Oct 31 '21 at 11:19

1 Answers1

0

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 then the usual

wantedFragment = navHostFragment.getChildFragmentManager().getFragments().first()
Foxhunt
  • 764
  • 1
  • 9
  • 23