问题
I've created a small app that has three fragments for top-level navigation through a BottomNavigationView. If you launch the app and click on a navigation button on the bottom nav, you are presented with an up button in the action bar. Here is the code for the activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
setSupportActionBar(toolbar)
val navController = navHostFragment.findNavController()
setupActionBarWithNavController(this, navController)
setupWithNavController(bottomNav, navController)
}
override fun onSupportNavigateUp(): Boolean
= findNavController(navHostFragment).navigateUp()
}
Here is a screenshot of the result. The app is launched on the home screen and all I've done is simply click the profile button from the BottomNavigationView.
I've tried listening to the BottomNavigationView's item selections and navigating manually using different NavOptions to no avail. Is there anything we can do to avoid showing an up button in the action bar while the user is navigating with a BottomNavigationView?
回答1:
Starting with 1.0.0-alpha07 you can use AppBarConfiguration
to configure that behaviour.
AppBarConfiguration
has a Builder constructor so you can reate a new Builder
with a specific set of top level destinations, referenced by their id
(this id
is the one you set on your navigation layout).
Create new AppBarConfiguration
:
val appBarConfiguration = AppBarConfiguration
.Builder(
R.id.navigationHomeFragment,
R.id.navigationListFragment,
R.id.navigationProfileFragment)
.build()
Then, instead of setupActionBarWithNavController(this, navController)
you need to call setupActionBarWithNavController(this, navController, appBarConfiguration)
This is the right way to handle top navigation behaviours.
回答2:
I had the same problem and I found a way to get the current fragment with the NavController.addOnNavigatedListener, so I applied the following logic within my Activity and for now it works for me
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
val navHost = supportFragmentManager
.findFragmentById(R.id.navHostFragment) as NavHostFragment
navHost.navController.addOnNavigatedListener { _, destination ->
val showButton = showUpButton(destination.id)
//Here occurs the magic
supportActionBar?.setDisplayShowHomeEnabled(showButton)
supportActionBar?.setDisplayHomeAsUpEnabled(showButton)
}
}
//Check according to your convenience which fragment id
//should show or hide the "Up Button"
private fun showUpButton(id: Int): Boolean {
return id != R.id.your_home_fragment && id != R.id.your_list_fragment
&& id != R.id.your_profile_fragment
}
And this is my project...
I do not know if it is the best option but if someone has any better suggestion, it will be welcome
回答3:
Use this instead of setupWithNavController
:
navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
@Override
public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
toolbar.setTitle(destination.getLabel());
}
});
Or the equivalent in Kotlin.
The only thing setupWithNavController
does is add a listener to change the toolbar title, and creates the up button. If you don't want the up button, just add a listener which changes the toolbar title only.
回答4:
If you are using NavigationUI.setupWithNavController
check out onNavDestinationSelected.
Inside your bottom navigation view menu.xml
, add android:menuCategory="secondary"
to the corresponding MenuItem
:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/profileFragment"
android:icon="@drawable/bottom_nav_icon_profile"
android:menuCategory="secondary"
android:title="@string/profile" />
来源:https://stackoverflow.com/questions/50301820/remove-up-button-from-action-bar-when-navigating-using-bottomnavigationview-with