0

I created a navigation bar and ,I didn't understand how to create an ID for each fragment in grph.

navigation graph:

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/nav_home">
    <fragment
        android:id="@+id/nav_home"
        android:name="com.mordechay.yemotapp.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" >
    </fragment>
    <fragment
        android:id="@id/nav_explorer"
        android:name="com.mordechay.yemotapp.filseExplorerFragment"
        android:label="fragment_filse_explorer"
        tools:layout="@layout/fragment_filse_explorer" />
</navigation>

in the activity:

<fragment
    android:id="@+id/nvgv_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintTop_toBottomOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph"
    />

And I wrote in the activity:

Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.nav_explorer);

It comes out null, what do I need to write to get the fragment?

I tried to get the fragment by writing the ID of the fragment found in the navigation graph. But I get null.

Mordechay
  • 21
  • 5

1 Answers1

1

You've already set your id's => android:id="@+id/nav_home" and android:id="@+id/nav_explorer".

At first, you need to declare your navigation controller.

val navigationController = findNavController(R.id.nvgv_fragment)

then you can check destination fragment with listener

navigationController.addOnDestinationChangedListener { _, destination, _ ->
    if (destination.id == R.id.nav_explorer) {
        // whatever you want to do
    }
}

But, if you only need an instance of displayed fragment, you can find the answer here: Current fragment instance

Sasa Arsenovic
  • 286
  • 2
  • 5
  • It did work. But I still couldn't get a fragment object, how did you manage to get a feagment object? I wrote it, but fragment is still null. if (navDestination.getId() == R.id.nav_explorer) { fragment = getSupportFragmentManager().findFragmentById(R.id.nav_explorer); } – Mordechay Nov 09 '22 at 19:13
  • I need this to implement this: https://stackoverflow.com/a/46425415/19169874 – Mordechay Nov 09 '22 at 20:32
  • 1
    Ok. You can find fragment instance with something like this: fun findFragmentInstance() { val fragmentHost = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) val fragmentInstance = fragmentHost?.childFragmentManager?.fragments?.last() if (fragmentInstance is YourFragment) { // Do whatever you want with fragment } } – Sasa Arsenovic Nov 10 '22 at 15:41
  • How is it in Java? – Mordechay Nov 13 '22 at 21:04
  • 1
    void findFragmentInstance() { Fragment fragmentHost = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment); List fragmentList = fragmentHost.getChildFragmentManager().getFragments(); Fragment fr = fragmentList.get(fragmentList.size() - 1); if (fr instanceof YourFragment) { // Do whatever you want with fragment } } // Be careful, fragmentHost, childFragmentManager and fragmentsList might be null. You need to check it to avoid NullPointerException. Hope this helps. – Sasa Arsenovic Nov 14 '22 at 15:54