Android Navigation Architecture Component - Get Current Visible Fragment

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.

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

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.

In Android Navigation Architecture, how can I check if current Fragment is the last one?

You can compare the ID of the start destination with the ID of the current destination. Something like:

override fun onBackPressed() = when {
navController.graph.startDestination == navController.currentDestination?.id -> showQuitDialog()
else -> super.onBackPressed()
}

Hope it helps.

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.

Get current fragment displayed in FragmentContainerView from MainActivity

You could add a OnDestinationChangedListener to the NavController inside you MainActivity

navController.addOnDestinationChangedListener { _, destination, _ ->
if(destination.id == R.id.your_savable_fragment) {
button.text = "Save"
//....
}
if(destination.id == R.id.your_editable_fragment) {
button.text = "Edit"
//....
}
}


Related Topics



Leave a reply



Submit