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.
Related
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.
It might be my misunderstanding of this libary itself, but I seem to be running into an issue. I am currently using Android Navigation Component (v3.4.0) and I am using a bottom navigation bar, which are linked together.
The issue I am see is that when I navigate to another bottom navigation item, a new fragment is created and a new viewmodel. None of the previous instances are destroyed, or at least onDestroy is not being called. I can test the logic by consuming a flow, when I navigate from bottom nav item 1, to bottom nav item 2, then back to bottom nav item 1, and emitted items from the flow are received twice, as there are now 2 instances of bottom nav item 1s view model. I can also log the hashcodes of each instance and see there's a difference.
When I navigate up a level, and back, the higher level fragment and viewmodel have their onDestroy and onCleared called.
Am I misunderstanding how this library is supposed to work?
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
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?
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