How to Retrieve Current Fragment in Navhostfragment

How I can retrieve current fragment in NavHostFragment?

Reference to the displayed fragment (AndroidX):

java

public Fragment getForegroundFragment(){
Fragment navHostFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
return navHostFragment == null ? null : navHostFragment.getChildFragmentManager().getFragments().get(0);
}

kotlin

val navHostFragment: Fragment? =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
navHostFragment?.childFragmentManager?.fragments?.get(0)

Here nav_host_fragment is an ID
of the fragment tag in your activity_main.xml with android:name="androidx.navigation.fragment.NavHostFragment"

Android Navigation Architecture Component - Get current visible fragment

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.

Android - Get current fragment

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()

How to get the currently displaying fragment in Nav Controller

I know there are already 2 answers on mine here but still have 1 more solution so I am writing it here. This is the code:

NavController navController = Navigation.findNavController(this, R.id.fragment);
int id=navController.getCurrentDestination().getId();
if(id==R.id.startGameFragment ){ // change the fragment id
selectedPosition(0);

}else if(id==R.id.gameFragment ){ // change the fragment id
selectedPosition(1);

}else if(id==R.id.endGameFragment ){ // change the fragment id
selectedPosition(2);

}

Call a current visible NavHostFragment's method from Activity

You can use the NavHostFragment to get the primaryNavigationFragment using its childFragmentManager.

val navHostFragment = supportFragmentManager.findFragmentById(R.id.your_nav_host_fragment_id) as NavHostFragment
val currentFragment = navHostFragment.childFragmentManager.primaryNavigationFragment

See if this works for you!

NavigationComponent: NavHostFragment inside a fragment

Try to get the NavHostFragment from the supportFragmentManager, and then get the navController upon.

val navHostFragment =
requireActivity().supportFragmentManager.primaryNavigationFragment as NavHostFragment
val navController = navHostFragment.navController

Thanks to @ianhanniballake you need to use the childFragmentManager as your current fragment is a child of a fragment.

So try to use instead:

val navHostFragment =
childFragmentManager.primaryNavigationFragment as NavHostFragment
val navController = navHostFragment.navController

UPDATE:

throws this java.lang.NullPointerException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment

So, the solution as from comments:

val navHostFragment = childFragmentManager.findFragmentById(R.id.nav_host_fragment) 
as NavHostFragment

And replace nav_host_fragment with the id of the inner NavHostFragment.

Android Navigation Architecture Component - Get current visible fragment

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.

Not getting current fragment id using navController.currentDestination?.id

The problem is you are getting currentDestination id only once because onCreate is called when the activity is created and you want to check that on which fragment currently you are when you click on Button so just move

this

val id = navController.currentDestination?.id

into on click of button

like this

        binding.myButton.setOnClickListener {
val id = navController.currentDestination?.id
if (id == R.id.firstFragment) {
navController.navigate(R.id.goto_secondFragment)
}
if (id == R.id.secondFragment) {
navController.navigate(R.id.goto_thirdFragment)

}
}

now what will happen is when you click on next or previous button it will first get the current destination id then it will check that in which fragment you are now.



Related Topics



Leave a reply



Submit