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)
}
Related
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.
I'm using NavigationComponent with BottomNavigationView.
I'm facing with a problem thats when i select first item in BottomNavigationView then go from fragment 1->2->3 after that i change selection to second item in BottomNavigationView. When i reselect first item in BottomNavigationView i'm losing fragment progress in first selected item.
The scheme of what is now
A scheme of what I want to achieve
How can i manage it?
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");
How to implement menu navigation drawer, when user click some item of menu,menu list swipe to new menu list. All of this happen in menu navigation drawer.
There is the screen
It is when menu navigation drawer open
This is when user clicked second item
And finally when user clicked some item
You can use fragment with listView. when you click on a item, A new Fragment will override the screen. There you need to manage to show content.
you also need to store all fragment in a backstack, so you can go back to previous fragment.
Demo Link :- https://www.dropbox.com/s/i2eu2hl117t6v1x/main.rar?dl=0
APK Link :- https://www.dropbox.com/s/8h2zr8ah2dxgik3/app-debug.apk?dl=0
Navigation view example
In reference to the above example of NavigationView, say I have "Go to Second item" button in First item layout. If I click on "Go to Second item" button, I am successfully able to go to the Second item layout, but the checked item in NavigationView remains in First item. How do I change the checked state of a MenuItem from another class which extends Fragment. If I directly click on Second item from the Menu, then the checked state changes accordingly, but how to change checked state from another class which extends Fragment.
NavigationView tutorial: Tutorial and Android documentation.
Wrap your menu items with the tag group with attribute android:checkableBehavior="single"
Call NavigationView.setCheckedItem() when you click the button.