How to make child fragments inherit bottom navigation bar from parent - android

I'm building an app using Android's Architecture Components in which I use the Navigation Component (with nav controller) to navigate to different destination fragments within my app. I currently have a navigation drawer defined in my Main Activity from which any of the child fragments can access through the toolbar. I want to implement a bottom navigation bar on one of my child fragments so that any of the destinations from that child fragment inherit its bottom nav bar. Here's a beautiful drawing illustrating what I'm trying to do.
So my question is essentially how do I make child fragments inherit a layout item from their parent? Would I need create an activity instead of Child Fragment 2 to host the bottom nav bar?

Related

AppBar with Fragments and Bottom Navigation Bar

Not sure if I'm fighting an uphill battle but I'm currently struggling with AppBar, Fragments and Bottom Navigation Bars.
I have a MainActivity that's a list view with an AppBar. It's created as a fragment with the intention to add another bottom navigation bar here in the future.
For now, when an item in the list is clicked it navigates to a detail view fragment. Here I still have the AppBar which is what I want however I would like to add a Bottom Navigation Bar. However when I navigate to the next fragment I lose my bottom navigation bar.
Is there a way to keep maintain Bottom Navigation Bars within Fragments? I tried breaking them out into activities but discovered then I lose my AppBar.
Is what I'm doing above feasible in Android?
Ideally I'd like this:
Do you have different nav graphs for different fragments? You need to have the detail view fragment in the same nav graph as the bottom navigation view. You don't need to have another item in bottom navigation but declare fragments that you want with bottom navigation in the same nav graph.

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.

Android navigation with activity and PagerView2

I have an issue with my Android app that uses Navigation and ViewPager2.
I've the main Activity which implements a Navigation Drawer and has its own Navigation Graph and it works fine with the rest of the application.
Then I have a second Activity that has a ViewPager2 which displays 3 different Fragments. Now the problem is that I need to navigate from the Main Activity to the Pager activity keeping the original Navigation infrastructure. I know that each activity has it's own Navigation Graph, but the second activity has a ViewPager2 control so that means it has 3 Fragments and I cannot specify a startDestination in the new Navigation Graph .... because there are 3 and here is the problem. I can display the new activity but the Application Bar with the Back button which would navigate to the original activity is not displayed. Any solution ?
You can not handle fragment of viewpager through nav graph, but you can create parent fragment called ViewPagerTabsHolderFragment and inside it, you will set up the view pager adapter fragments then you can set the nav graph start destination to this ViewPagerTabsHolderFragment

Android Navigation Component Recreates Bottom Navigation Fragments

Android navigation component replaces fragments present in the app and calls onlyonDestoryView(), but the fragments in bottom navigation get destroyed completely ,onDestory() is called.
The Question is:
How can I prevent bottom navigation from completely destroying fragments that belong to bottom navigation and not calling onCreate() everytime I switch to one of them.
In view pager we had offScreenLimit that hold the fragments without destorying their views.

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