Android Navigation Architecture Component - Get current visible fragment

Clash Royale CLAN TAG#URR8PPP
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
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)
supportFragmentManager.findFragmentById(R.id.nav_host)
returns a NavHostFragment and I want to retrieve my shown 'MyFragment`.
NavHostFragment
Thank you.
I needed from my fragment to start a camera intent, take a picture and use the image taken. To do so, I've started an activity and waited for result in the
onActivityResult of my main activity. Since I could not get the fragment, I simply moved all this into the fragment itself and seems to work.– Alin
Jul 18 at 8:14
onActivityResult
Do you want to perform an operation on that fragment object. Or just you want which fragment is shown ?
– Ranjan Das
Jul 30 at 8:07
Have you found a way?
– Andrei Toader
Aug 7 at 17:29
4 Answers
4
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.
It's great to see a working solution. I gave up on this and started using a shared viewModel between the activity and fragment, This means that I can pass events between them without the need of callbacks or direct access.
– Alin
Aug 7 at 19:07
can you please check with getChildFragmentManager() call with
getChildFragmentManager()
NavHostFragment fragment = supportFragmentManager.findFragmentById(R.id.nav_host);
MyFragment frag = (MyFragment) fragment.getChildFragmentManager().findFragmentByTag(tag);
Does not seem to work. Thank you for your time
– Alin
Jul 17 at 19:21
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.getCurrentDestination().getId()
It returns the ID in navigation file. Hope this helps.
Thank you for your answer. I needed to retrieve the fragment so I can interact with it. Since navController can't return it, I ended using a shared viewmodel to talk between activity and its fragments
– Alin
Jul 28 at 20:25
In your activity define a fragment object. And from each fragment call this method by passing fragment object, like
So the static fragment object will be overridden by current showing fragment every time.
//method in MainActivity
private static Fragment fragment;
public void setFragment(Fragment fragment)
this.fragment = fragment;
//And from your fragment class, you can call
((<yourActivity in which fragment present>)getActivity()).setFragment(<your fragment object>);
//if you want to set it directly;
((<yourActivity in which fragment present>)getActivity()).fragment=<your fragment object>;
You can also set callback from fragment to activity, for a better approach and pass your current fragment object.
Thank you for your time. I was looking for something to use from the Navigation component itself.
– Alin
Jul 30 at 20:54
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
There is no way to retrieve the current fragment stackoverflow.com/a/50689587/1268507 What are you trying to achieve? maybe there is some other solution for it.
– Alex
Jul 18 at 5:27