how to call navigation.navigate inside recyclerView item - android

I have a recyclerView inside a fragment, and when I click an item I want to navigate to next fragment. At the first time I click, it's OK, but after getting back to this fragment and clicking the same item again my application crashes. Could anyone please tell me how to call navigation.navigate inside recyclerView item?

If you are using the navigation compenent, and inside the recyclerview, you need to go to another class, use the following:
Navigation.findNavController(itemView).navigate(
UIFragmentVideoListDirections.aVideoList2Video((aFile.absolutePath))
)
Please try and let me know.
Added Answer:
NavHostFragment — implementation of NavHost for creating fragment destinations. It has its own NavController and navigation graph. Typically you would have one NavHostFragment per activity.
Navigation — helper class for obtaining NavController instance and for connecting navigation to UI events like a button click.
NavigationUI — class for connecting app navigation patterns like drawer, bottom navigation or actionbar with NavController.

Related

Switch tab on button click with Bottom Navigation and Navigation component

I have a very simple app that consists of three Fragments and a Bottom Navigation bar, created by using "New Project -> Bottom Navigation Activity" in Android Studio. The first Fragment holds a button, which should take me to the second Fragment, like if the middle button of the Bottom Navigation bar was clicked.
Is there a "standard" way to do this?
I have tried:
using launch(...) of the Navigation component, which seems to launch the Fragment with its own back stack and breaks the bottom navigation.
using setSelectedItemId(...) in different ways, which either results in an exception or breaks bottom navigation in different ways.
In this post, someone asks the exact same question, but it is marked as a duplicate. I fail to find the answer, especially concerning Navigation component.
Clicking the Button should have the same effect as if the user taps the corresponding item in the bottom navigation. So you need to call setSelectedItemId() on the BottomNavigationView. This can only be done in the Activity displaying the BottomNavigationView.
One option is to introduce a shared ViewModel with
a LiveData to be observed by the Activity
a function onButtonClicked() to be called by the OnClickListener of your Button which will update the LiveData
Once the LiveData observer fires, your Activity can call
binding.navView.selectedItemId = R.id.navigation_dashboard
Please note that for passing information about events like this one should choose some data type which can be invalidated after use. See for example LiveData with SnackBar, Navigation and other events (the SingleLiveEvent case)
Paste this code from where you want to go to the second fragment
Fragment fragment = new DashboardFragment();
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.beginTransaction().replace(R.id.frame_layout, fragment).commit();
For more information click here

BottonNavigationView Icon not changing on back press

I am using navigation component for bottom navigation component.
bottomNavbar.setupWithNavController(navController)
now this is working fine but when I hit the back button, it is returning to the home page but the icon is not changing, it is stuck in the previous selected fragment. I have three fragments and I have implemented navbar separately in all those fragments, here's the code for those three fragments.
settings fragment
val bottomNavbar = view.findViewById<BottomNavigationView>(R.id.bottomNavbar)
bottomNavbar.setupWithNavController(navController)
search fragment
val bottomNavbar = view.findViewById<BottomNavigationView>(R.id.bottomNavbarSearch)
bottomNavbar.setupWithNavController(navController)
chat fragment
val bottomNavbar = view.findViewById<BottomNavigationView>(R.id.bottomNavbar)
bottomNavbar.setupWithNavController(navController)
here search fragment is my home fragment.
Is there a mistake in my implementation or should I just switch to the old way of implementing bottom navigation view.
any help is appreciated. Thanks
Make sure to have the same ids in the bottomNavView menu that matches
the corresponding ones in the navGraph of the bottomNavView fragments.
check this answer
It seems you have added BottomNavigationView on all three fragments which should ideally be on Activity's view below your fragment/FragmentContainerView where you have defined your navGraph.
To fix it you need to remove BottomNavigationView from all 3 fragments and add it on Activity using the following basic structure.
Activity XML structure:
<constriantLayout>
<fragment/> //with navGraph
<bottomNavigationView/>
</constraintLayout>
and onCreate of Activity use the code setting up BottomNavigationView
val bottomNavbar = findViewById<BottomNavigationView>(R.id.bottomNavbar)
bottomNavbar.setupWithNavController(navController)

BottomNavigationView in child fragment in Navigation Component

I want to achieve something like this image flow in android navigation component. Where the Dashboard Fragment is the start destination. And from here i can navigate to another fragment which have a bottom navigation view. Is this possible using a single nav graph and a single activity? What is the best way to achieve something like this?
You can use BottomNavigationView
val childNavView: BottomNavigationView = binding.nav1View
val navController = NavHostFragment.findNavController(childFragmentManager.findFragmentById(R.id.navHostFragment) as NavHostFragment)
childNavView.setupWithNavController(navController)
In case someone's still looking for a solution.
Just use two separate navigation graphs. And when navigating, pick the right navigation controller object. You can use a single activity of course, with nested fragments. So your "Bottom nav page" will have its own navigation container with child fragments in it.
Moreover, you can also navigate from the inner nodes to outer ones. In that case you need to reference the parent fragment ("Bottom nav page") from the child fragment and then obtain its navigation controller like this:
parentFragment?.parentFragment?.findNavController()
Here the first parentFragment is NavHostFragment and its parent is your actual parent fragment ("Bottom nav page").
If you want Page 1-3 to have options for further fragments to navigate inside (i.e. Page 1.1, 1.2 for Page 1, etc), then you need a navgraph for each Page (so 3 total), I would also make them all the root of their own navigation, would make it a lot easier to implement, and just replace the dashboard fragment with an activity if it's for signing in for example.

Navigate from ViewPager2 to another fragment using jetpack

I have single activity, FragmentHome contains viewpager2. Inside of viewpager there is FirstFragment.
when I want to navigate from FirstFragment to SecondFragment which is not in viewpager2
it throws
java.lang.IllegalArgumentException: navigation destination com.example.mymessangerfcm:id/action_FirstFragment_to_SecondFragment is unknown to this NavController
Dear community how to handle such navigation, and why the exception is thrown?
you should use the Global Action. When you are inside the viewpager, actually you are in a different navigation. So, you should make your action as Global. You can check the Global Action from official website :
https://developer.android.com/guide/navigation/navigation-global-action
After the making action as global, you can navigate like this;
findNavController().navigate(R.id.action_global_FirstFragment_to_SecondFragment)
Note: If you don't see global action after the doing it, rebuild your project.
I got below IllegalStateException when calling Navigation.findNavController(view) with ViewPager2. However, the same setup working fine with old ViewPager.
java.lang.IllegalStateException: View android.widget.LinearLayout does not have a NavController set

Navigation Library double navigation UI components

I'm trying to set up navigation in my application, it works well for simple things but I can't get it to work for some of the clients requirements, what I'm trying to do right now is set up a navigation graph based on the one activity many fragment idea, unfortunately each of these fragments have their own sub navigation (requirement) so for instance my main activity hosts my main nav graph and swaps out fragments based on the navigation views menu's id's using the NavigationUI library, but the first fragment shown holds a bottom navigation view with just 2 fragments (don't get me started on why this is poor design) so I tried to give this fragment its own nav graph, this works in that it shows the home fragment but it doesn't allow me to navigate using said graph its always trying to get the main graph for the navigation view drawer regardless of the view I try to find it with, so I tried to nest a graph in the main graph which again works but this draws the fragment over my bottom navigation view making it impossible to see or press it, so my question is how would I control 2 NavigationUI components, my navigation view (drawer) and bottom navigation view? do I use 2 nav graphs or nest the nav graph? and then how do I get a handle on them as passing the view doesn't seem to work in this instance
So it was looking for the nav graph in the heirarchy which was missing my nav graph for this layout and finding the one in the main activity i found i could call a nav controller by its id from a fragment like this
NavController navController = Navigation.findNavController(requireActivity(), R.id.main_nav_host);
which means i could use the nav graph i wanted and solve my issue

Categories

Resources