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
Android Textview: Setting the Background Color Dynamically Doesn't Work
Android: How to Convert Whole Imageview to Bitmap
How to Fix "Failed to Sync Vcpu Reg" Error
Google Maps API Version Difference
Android - Hide All Shown Toast Messages
Android Fab Icon Always Black with Materialcomponents Theme
How to Use Bluetooth in Android Emulator
What's the "Dot" for When Registering an Activity
Missing Styles. Is the Correct Theme Chosen for This Layout
How to Correctly Use Google Plus Sign in with Multiple Activities
How to Use Android Emulator for Testing Bluetooth Application
How to Tell If a Closed Path Contains a Given Point
Http Status Code in Android Volley When Error.Networkresponse Is Null
Emulator Appearing Offline on M1 MAC After the Last Update of Arm64-V8A
React Native Adb Reverse Enoent
Adding Watermark Bitmap Over Video in Android: 4.3's Mediamuxer or Ffmpeg