Navigate from ViewPager2 to another fragment using jetpack - android

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

Related

How to setup Multiple nested FragmentContainerViews with respective navigation graphs?

I was looking into converting my app to single activity as per google's guidelines. (Using Kotlin)
Let's say i have FragmentContainerView1 in my activity with no toolbar or anything else.
This view is used to navigate between login screen,sign up and display the main fragment.
Inside the main fragment, I have another FragmentContainerView2 which should display according to a BottomNavigation view inside main fragment.
But then i have some elements inside the fragments in FragmentContainerView2, that need to perform actions in FragmentContainerView1.
How do i achieve this?
Picture :
Here mainFragment contains the BottomNavigation,of which mainMenuFragment is a destination,which has to perform actions in FragmentContainerView1. How can I get a reference of the inner nav controller of the BottomNavigation?
Edit 1: navController=Navigation.findNavController(view.findViewById(R.id.fragmentContainer)) doesn't work, still returns same outer nav controller.
Not a Complete Solution, But the answer I found was to keep one of the containers as a <fragment/> and the other as FragmentContainerView.
And to get the inner nav controller use:
innerNavController= (activity as AppCompatActivity).findNavController(R.id.innerFragmentContainer)
Only works with that specific command and one of the containers being <fragment/>.
Also after getting the innerNavController, Any type of call with findNavController() gives the inner nav controller.
To get the outerNavController use:
outerNavController = (activity as AppCompatActivity).findNavController(R.id.outerFragmentContainer)

Using ViewPager2 in an Existing Fragment

I have Created a Fragment for my bottom Tab bar, The tab bar has four fragments, In one of my fragments, I am in need of Slider to view testimonials of people one by one.
In this case, I have used the ViewPager2 functionality to work on the slider, I have all the steps to create Viewpager2 from this video
https://youtu.be/KwihiADN-0k
After following all the steps I have getting an error stating
java.lang.IllegalStateException: viewpager1 must not be null (viewpager1 is my id of Viewpager2 function)
You should define your viewpager1.adapter = viewpager1Adapter not in onCreateView(), but in onViewCreated() and your issue will be solved.

how to call navigation.navigate inside recyclerView item

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.

Navigation Component is not replacing current fragment activity

So, I'm trying to navigato from one Fragment to another, I have used this line
val action = MapsFragmentDirections.nextAction(marker?.title!!)
findNavController().navigate(action)
this navigation goes to another Fragment, But in the time it reachs that fragment, I see the activity below my fragment and my fragment is transparent with the recyclerview above.
I have researched and found that I need to se replace with FragmentTransaction, but I think that is done in the background of Navigation Component.
The problem is that when inflating my other fragment, I can see my activity below the view of the fragment.
A temporary fix is to put the background of the RecyclerView as blank, but I dont want to fix it that way, I want it to be inflated like a normal fragment without seeing nothing under it
My problem is exactly this one : Fragment is transparent and shows Activity below
But I need to solve it with Navigation Component, and I dont see any method that helps me do that.
Or does I need to set all my view inside a fragment> tag in my xml ?
Thanks
When using NavController, you shouldn't be doing any separate FragmentTransactions at all - as long as you've added a NavHostFragment to your XML and are using the app:navGraph attribute to specify your graph, the NavController does all the work on switching between Fragments and making sure they don't overlap.

Can i pass an Instance of a FragmentActivity into a Fragment transaction

I’m trying to do a tab layout, but before I’ve a navigation drawer that contains some menu. In one of those items I want to display a Tab activity or let’s say view. My problem is in the replace method we should pass a Fragment and in a tabbed view you can't do it with a Fragment you should have an activity or a FragmentActiviy. Now I’m trying to found a solution to this.
There is nothing that actually forbids using a ViewPager inside a Fragment, but I'd try to avoid going down that road. I fear for the UX, having both a navigation drawer and tab navigation is very confusing.
If you must do it nonetheless, you can find an example of using ViewPager on the official documentation page here, it is using Activity but the only main difference I see when implementing that in a Fragment is that the FragmentManager you use should be the one obtained calling getChildFragmentManager inside the main Fragment.

Categories

Resources