Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Navigation Architecture Component - Get current visible fragment

Before trying the Navigation component I used to manually do fragment transactions and used the fragment tag in order to fetch the current fragment.

val fragment:MyFragment = supportFragmentManager.findFragmentByTag(tag):MyFragment

Now in my main activity layout I have something like:

<fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/nav_host"
        app:navGraph= "@navigation/nav_item"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:defaultNavHost= "true"
        />

How can I retrieve the current displayed fragment by the Navigation component? Doing

supportFragmentManager.findFragmentById(R.id.nav_host)

returns a NavHostFragment and I want to retrieve my shown 'MyFragment`.

Thank you.

like image 924
Alin Avatar asked Sep 25 '22 15:09

Alin


People also ask

How do you get current visible fragments?

To get the current fragment that's active in your Android Activity class, you need to use the supportFragmentManager object. The supportFragmentManager has findFragmentById() and findFragmentByTag() methods that you can use to get a fragment instance.

How do you get a fragment instance in navigation component?

To add a new destination using the Navigation Editor, do the following: In the Navigation Editor, click the New Destination icon , and then click Create new destination. In the New Android Component dialog that appears, create your fragment. For more information on fragments, see the fragment documentation.

What is a FragmentManager?

FragmentManager is the class responsible for performing actions on your app's fragments, such as adding, removing, or replacing them, and adding them to the back stack.


4 Answers

I managed to discover a way for now and it is as follows:

NavHostFragment navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host);
navHostFragment.getChildFragmentManager().getFragments().get(0);

In case of course you know it is the first fragment. I am still investigating a way without this. I agree it is not the best way but that should be something for now.

like image 158
Andrei T Avatar answered Oct 19 '22 10:10

Andrei T


There is no way I can find to retrieve the current fragment instance. However, you can get the ID of lastly added fragment using the code below.

navController.currentDestination?.getId()

It returns the ID in navigation file. Hope this helps.

like image 79
slhddn Avatar answered Oct 19 '22 10:10

slhddn


You should look the childFragmentManager primaryNavigationFragment property of your nav fragment, this is where the current displayed fragment is referenced.

val navHost = supportFragmentManager.findFragmentById(R.id.main_nav_fragment)
    navHost?.let { navFragment ->
        navFragment.childFragmentManager.primaryNavigationFragment?.let {fragment->
                //DO YOUR STUFF
        }
    }
}
like image 57
Nicolas Pietri Avatar answered Oct 19 '22 11:10

Nicolas Pietri


This seems like the cleanest solution. Update: Changed extension function to property. Thanks Igor Wojda.

1. Create the following extension

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager

val FragmentManager.currentNavigationFragment: Fragment?
    get() = primaryNavigationFragment?.childFragmentManager?.fragments?.first()

2. In Activity with NavHostFragment use it like this:

val currentFragment = supportFragmentManager.currentNavigationFragment
like image 31
Aleksandar Ilic Avatar answered Oct 19 '22 10:10

Aleksandar Ilic