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
How to Access Data/Data Folder in Android Device
Check If an Email Exists Before Creating a User Firebase Android
How to Set Transparency of a Background Image Android Xml File
How to Remove Focus from Edittext When User Is Done Editing
Android Studio: Emulator Is Running But Not Showing Up in Run App "Choose a Running Device"
How to Dynamically Hide a Menu Item in Bottomnavigationview
Videoview to Match Parent Height and Keep Aspect Ratio
Add Text to Image in Android Programmatically
How to Disable Edittext in Android
When Does Sqliteopenhelper Oncreate()/Onupgrade() Run
How to Format Credit Card Expiry Date in Mm/Yy When Entered by User
How to Get Last Record from Sqlite
How to Delete or Remove Cardview from Recyclerview - Android Studio
Android No on Screen Keybord ( Only Voice )
How to Convert Image to Zpl Code for Printing Using Zebra Printer
How to Change Styling of Textinput Placeholder in React Native
How to Get Data from Edit Text in a Recyclerview
Cannot Resolve Symbol Appcompatactivity - Support V7 Libraries Aren't Recognized