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)
Related
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.
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.
i'm quite new to drawer material and i have trouble understanding some things:
I need to create an Activity with a Fragment on it. Different selections on the Drawer must replace the current Fragment with another one, but is the Drawer something in the fragment or it is one for the activity itself. More specifically is the Drawer living in the Fragment and if not is it possible to create it in the Fragment. I am asking this cause i see that when initiating the Drawer you need to fill parent activity. Also when i tried to use the Navigation Drawer template in Android Studio i didn't had the Use a Fragment checkbox.
There are many ways to achieve what you want. But I think the simplest way is:
1) DrawerLayout view should reside in the activity (probably as the base layout).
2) When you click on an item in the draw 2 things happen:
The fragment is replaced (you have one layout to contain the fragment and you just replace the fragment in it).
The items inside the drawer update (if you are making a list you would simply set the data and call notifyDataSetChanged().
Don't forget to save your state so it can recover in case the Activity is recreated.
I created a navigation drawer and on the top I would like to place a fragment containing information about the logged in user (kind of like what google apps have.) I made the fragment and put it inside the navigation drawer and that works fine, but I need to pass it data from the parent fragment. Thus I tried using
(UserOverviewFragment)getChildFragmentManager().findFragmentById(R.id.user_fragment);
But that returns null everytime. How can I get the Fragment object?
Thank you.
You need something called RecyclerView. Follow this excellent guide to do it.
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.