Navigation Component not calling onDestroy for bottom nav fragments - android

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?

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

Is there a way to force the navigation component to recreate a fragment when the bottom navigation is reselected

So basically, I have a bottom navigation using the Navigation component for Android. I need to refresh my fragment when reselecting the bottom navigation but when I reselect the item in the bottom nav, it keeps the state of the fragment the same. Is it possible to use the navcontroller to force the fragment to refresh?
You can use a callback and when reselected just call the function again which is fetchingData() and set it to UI.
Without seeing your code, it's difficult to help you with the exact answer.

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

Managing Action Bar titles when using fragments and the backstack

My app has an activity which uses the FragmentManager to push and pop fragments onto the backstack. However, when the backstack has 2 fragments and the last fragment is popped, I do not how to detect this to set the title of the Action Bar appropriately. I was hoping there was some method on a fragment I could override to determine when it becomes visible, but none of those suggested e.g. uservisiblehint, onHiddenChanged, etc. are called. How can I either:
• determine when a fragment becomes visible; OR
• effectively manage action bar titles when using fragments on a backstack?
You can add a addOnBackStackChangedListener which will get called every time back stack changes.
Inside this function you can simply get the topmost fragment and call onResume for it.
For more information you can refer to following link:
Fragments onResume from back stack

Categories

Resources