Restore menu items states using Navigation with BottomNavigationView + FloatingActionButton (FAB) - android

How can I setupWithNavController my BottomNavigationView with a FloatingActionButton (FAB) on top?
Thanks StackOverflow for hating me and not letting me display photos. :)
According to the Navigation library releases…
Starting in Navigation 2.4.0-alpha01, the state of each menu item is
saved and restored when you use setupWithNavController.
This works like a charm, however, when I setup my BottomNavigationView and press the FAB, the states of the current menu items are reloaded. How can I achieve the same behavior of clicking on any menu item with the FAB?
binding.activityMainBottomNavigationView.setupWithNavController(navController)
binding.activityMainFloatingActionButton.setOnClickListener {
// TODO: Fix navigation.
navController.navigate(R.id.homeFragment)
binding.activityMainBottomNavigationView.selectedItemId = R.id.homeFragment
}
As I understand, calling navController.navigate() may be resetting the current nav_graph used by the fragmentContainerView and that's why the current menu items are being reloaded. Is there any way to avoid this?

After a week of several attempts, I achieved the desired purpose using onNavDestinationSelected from NavigationUI.
binding.activityMainBottomNavigationView.setupWithNavController(navController)
binding.activityMainFloatingActionButton.setOnClickListener {
NavigationUI.onNavDestinationSelected(
binding.activityMainBottomNavigationView.menu.findItem(R.id.homeFragment),
navController
)
binding.activityMainBottomNavigationView.selectedItemId = R.id.homeFragment
}
In this way, the states of the fragments and navigation survive! 🤠
Cheers! 🍾

Related

BottonNavigationView Icon not changing on back press

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)

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.

When using Navigation Component, how do I set an argument at runtime for a destination accessed via navigation view menu?

I have a navigation drawer with a navigation view with menu items and I'm using navigation component. How do I specify an argument for a destination at runtime when a certain menu item in the navigation view is clicked?
I know I can set default arguments in the nav graph but this is not what I want to do because the argument passed needs to be decided at runtime. For toolbar menu items, I have no issue as I'm able to override onOptionsItemSelected. My issue is specifically with menu items in the navigation view.
I have tried the approach here: https://stackoverflow.com/a/54086631/10933532 but I am unable to create a NavArgument as described. The NavArgument class does not give me the public Builder() constructor.
I set up my navigation view with the navigation controller in mainactivity onCreate like this:
NavigationUI.setupWithNavController(navigationView,navController);
I also do this to set up my toolbar with the navigation controller:
NavigationUI.setupWithNavController(collapsingToolbarLayout,toolbar,navController,appBarConfiguration);
Can someone please point me in the right direction?
You can use a custom NavigationItemSelectedListener.
Pay attention because in this way you are removing the original listener. You have to call NavigationUI.onNavDestinationSelected() to trigger the default behavior.
navigationView.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.xxxxx -> {
// handle click
true
}
}
//Call the original listener
NavigationUI.onNavDestinationSelected(it, navController)
drawerLayout.closeDrawer(GravityCompat.START)
true
}

Binding different menu with different fragment with Navigation component

I have an activity with BottomNavigationBar.
I show app logo in middle in toolbar by default.
Now I have to show SearchBar on entire toolbar when one of the bottomNavigation item is selected. Also, I want to revert to default toolbar view (one with logo in middle) on selecting any other bottomNavigation item.
How can i do this with Navigation component?
If I have to use ViewSwitcher or ActionMode, the whole idea of navigation component will have to be dropped as I can handle a few fragment transaction by myself.
Help me out here.
There's two approaches to do this:
1) Have each Fragment implement its own Toolbar
This approach gives you the ultimate flexibility in what each Fragment is responsible for, but is more suitable if you have many different types of Fragments or need scrolling behavior that is different for each Fragment.
2) Use a OnNavigatedListener to change your Activity's Toolbar
The NavController allows you to attach any number of OnNavigatedListener instances, which give you a callback whenever the current destination / item changes.
This allows you to write code in your Activity such as:
navController.addOnNavigatedListener { navController, destination ->
if (destination.id == R.id.search_destination) {
// Update your Toolbar to be a SearchBar
} else {
// Reset it back to a standard Toolbar
}
}

Android Jetpack Navigation How to handle the Toolbar and BottomNavBar content

I am a bit confused on how the Navigation component fits in the app behavior. It all looks nice and shiny in tutorials where you don't do things too complex but when implementing in real app, things seem different.
Before Navigation
Before implementing navigation I had to manually run fragment transactions. In order to do this, my fragment would implement an interface onFragmentAction which passed a bundle to the main Activity and in the activity based on the actions, replace the current fragment with another one.
The second part that needs handling is the top toolbar and the BottomAppBar. For instance BottomAppBar needs to have the FAB aligned differently on some fragments or hidden in others. Also the top ToolBar needs to be expanded on some or collapsed on others. To do this, I listened to FragmentManager.OnBackStackChangedListener and based on the fragment tag getSupportFragmentManager().getBackStackEntryAt(size - 1).getName() change the layout accordingly.
With Navigation
The first part seems to be easy to do: pass params and start new fragments. But I have no idea if navigation can handle the toolbars management or I need to keep managing it from my Activity.
Even though Alex's solution works, I would not recommend it for the purpose of managing the toolbar.
The toolbar should be part of your fragment's layout and each fragment should manage its own toolbar. you can inflate different menus for each fragment. even in the case of wanting to have the toolbar in the activity, I would recommend getting a reference to the toolbar form activity (through an interface) and then adding and manipulating its items in the fragment itself.
This would decouple your activity and fragment (which is one of the goals of having navigation graph and a router). a good rule of thumb is that imagine you want to remove the fragment, then you should not need to make any change to the activity.
The toolbar title is set based on 'label' value inside navigation graph, if you want to do something different with toolbar or BottomAppBar you can add addOnNavigatedListener inside your activity, and based on current destination do something.
findNavController(nav_host_fragment).addOnNavigatedListener { controller,
destination ->
when(destination.id) {
R.id.destination1 -> {
//Do something with your toolbar or BottomAppBar
}
R.id.destination2 -> {
//Do something with your toolbar or BottomAppBar
}
}
}
In your fragment:
NavController navHostFragment = NavHostFragment.findNavController(this);
NavigationUI.setupWithNavController(toolbar, navHostFragment);
When I click item on list item (Explore Fragment) it will negation to DetailFragment and when I click the back button on the toolbar it will return MainFragment.
if you want to reach to another fragment by calling on menu item, you must give to item id the the same id which is in the destination id.
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return
item.onNavDestinationSelected(findNavController(R.id.nav_host_fragment))
|| super.onOptionsItemSelected(item)
}
<item android:id="#+id/dailyInfoFragment"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
<fragment
android:id="#+id/dailyInfoFragment"
android:name="com.example.sonyadmin.infoPerDay.DailyInfoFragment"
android:label="fragment_daily_info"
tools:layout="#layout/fragment_daily_info"
/>

Categories

Resources