I am facing a challenge in the app that i am developing.
I have a BottomNavigationView with 4 Fragments:
Home Feed Notification Profile
Home is the home fragment of my NAV GRAPH.
Lets suppose Home has a button and on click of it, it goes to CartFragment. Now i click to Feed by using BottomNavigationView. So now, when i click on home fragment again from BottomNavigation view...i see the CartFragment whereas i want to see the HomeFragment. And it doesn't even show the HomeFragment tab active on the bottomNavigationView, it is like it completely replaces it.
I am not able to switch to my original fragment whenever i click on a tab from BottomNavigationView
If you want to reset your backstack when you press one of the bottom nav items, use below code where you define bottom nav view and this will solve your problem:
yourBottomNavView.setOnItemSelectedListener { item ->
NavigationUI.onNavDestinationSelected(item, yourNavController)
yourNavController.popBackStack(item.itemId, inclusive = false)
true
}
Just change yourBottomNavView and yourNavController with yours.
Related
I have bottomNavigation in my android app as shown below in picture. It works fine if I just click on home, cart and other bottomNavigation Items.
As you can see in picture that user can navigate to all categories in home. If I navigate to AllCategoriesFragment then in home item will be selected in bottomNavigation which is correct behavior. But if I go to cart and then click again on home then it shows AllCategoriesFragment which is also correct behavior but in BottomNavigation it doesnt highlight the home items, instead it keep cart item highlighted.
How I can get rid of this issue? Or how I can highlight the bottomNavigation selected item programmatically?
I did it programmatically like by getting activity reference inside fragment andd then getting navView from it and afterwards set the check state of the item to true
activity?.let {
val navView = it.findViewById<BottomNavigationView>(R.id.nav_view)
navView.menu.getItem(0).setChecked(true)
}
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
I am using navigation component for bottom navigation component.
bottomNavbar.setupWithNavController(navController)
now this is working fine but when I hit the back button, it is returning to the home page but the icon is not changing, it is stuck in the previous selected fragment. I have three fragments and I have implemented navbar separately in all those fragments, here's the code for those three fragments.
settings fragment
val bottomNavbar = view.findViewById<BottomNavigationView>(R.id.bottomNavbar)
bottomNavbar.setupWithNavController(navController)
search fragment
val bottomNavbar = view.findViewById<BottomNavigationView>(R.id.bottomNavbarSearch)
bottomNavbar.setupWithNavController(navController)
chat fragment
val bottomNavbar = view.findViewById<BottomNavigationView>(R.id.bottomNavbar)
bottomNavbar.setupWithNavController(navController)
here search fragment is my home fragment.
Is there a mistake in my implementation or should I just switch to the old way of implementing bottom navigation view.
any help is appreciated. Thanks
Make sure to have the same ids in the bottomNavView menu that matches
the corresponding ones in the navGraph of the bottomNavView fragments.
check this answer
It seems you have added BottomNavigationView on all three fragments which should ideally be on Activity's view below your fragment/FragmentContainerView where you have defined your navGraph.
To fix it you need to remove BottomNavigationView from all 3 fragments and add it on Activity using the following basic structure.
Activity XML structure:
<constriantLayout>
<fragment/> //with navGraph
<bottomNavigationView/>
</constraintLayout>
and onCreate of Activity use the code setting up BottomNavigationView
val bottomNavbar = findViewById<BottomNavigationView>(R.id.bottomNavbar)
bottomNavbar.setupWithNavController(navController)
I'm using Android Navigation Component. There is one activity with FragmentContainerView(NavHostFragment) with simple navigation flow by common_navigation_graph
But on one fragment there is a nested NavHostFragment and BottomNavigationView. Reason for that - I want to have 3 tabs on the fragment. Bottom navigation configure with own navController and separate navigation_graph
val navController = activity?.findNavController(R.id.navHostViewPagerView)
binding?.bottomNavigation?.setupWithNavController(navController!!)
On each Tab there is a one more nested fragment with own FragmentContainerView and it's own small tab_navigation_graph
Navigation scheme looks:
[enter image description here][1]
[1]: https://i.stack.imgur.com/gmXOY.png
In general it works pretty well. Navigation between tabs, navigation in some selected Tab works well, back stack works well
override fun handleOnBackPressed() {
view?.findNavController()?.popBackStack()
}
But since there was a navigation to some next fragment from common_navigation_graph(SomeNextFragment on the picture) the backStack flow will be broken.
I will be able to return to fragment with Tabs, but backStack flow for nested fragments in the Tab will not work. Only the last fragment will be displayed(Tab1.2) Transition by Blue arrow not working.
On handleOnBackPressed() view==null and fragment.id==0. Fragment was destroyed
Question is how to save ability to navigate through nested Tab fragments on come back from other fragmnet from common_navigation_graph
Each tab should hold its own back stack.
https://github.com/googlesamples/android-architecture-components/tree/master/NavigationAdvancedSample
Started new project from navigation drawer activity template. By default, all drawer menu items are at the same navigation level. All fragments display hamburger menu button, that shows drawer.
I need to keep all items in drawer, but place Home as top level item and others as it's children like this:
Tried to overwrite toolbar?.setNavigationOnClickListener { } in Gallery fragment, but it also affects Home fragment and I didn't find how to restore default behavior.
How can I set Home as navigation parent for others or how to set navigation click listener to only one fragment?
For example you have set tag for every fragment to trace when you start new fragment and now check with Home and set click event..
Fragment home = getSupportFragmentManager().findFragmentByTag("home");