I am actually using the new Architecture components in my app, and I've setup the navigation component. I have a navigation drawer and I want to use it with. I've set it up, but I'm facing some issues :
1 - The drawer doesn't close itself. The menu works and navigate to the right place, but it doesn't close it after the navigation. I had to add a destinationChangedListener to close it myself.
navController.addOnDestinationChangedListener { _, _, _ ->
if (drawer_layout.isDrawerOpen(GravityCompat.START))
drawer_layout.closeDrawer(GravityCompat.START)
}
In the codelabs, the drawer close itself, and I don't really understand why.
2 - The up button opens the drawer. When I navigate to a non top-level fragment, the menu icon change to the up arrow, but when I click on it, it opens the drawer menu instead of going back to the previous destination.
3 I would like, for some items of my menu, to set a custom on click. All the buttons on my menu are not for navigate to the app, and I don't know where I can override the behavior of the navController to tell it what to do when he can't navigate.
Here's the relevant code :
ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/layout_app_bar"/>
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_marginTop="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/app_navigation" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<include layout="@layout/layout_navigation_view"/>
</androidx.drawerlayout.widget.DrawerLayout>
</FrameLayout>
Layout_app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:theme="@style/AppTheme.AppBarOverlay"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/language_main_color"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/appAction" >
<item
android:id="@+id/bridgeFragment"
android:icon="@drawable/ic_home"
android:title="@string/home" />
<item
android:id="@+id/categoryFragment"
android:icon="@drawable/ic_categories"
android:title="@string/categories" />
<item
android:id="@+id/favoriteFragment"
android:icon="@drawable/ic_favorite"
android:title="@string/favorites" />
<item
android:id="@+id/unlimited"
android:icon="@drawable/ic_unlimited"
android:title="@string/dialog_ask_unlimited_title" />
<!--<item
android:id="@+id/settings"
android:icon="@drawable/ic_settings"
android:title="@string/settings" /> -->
</group>
<group android:id="@+id/globalAction">
<item
android:id="@+id/rate"
android:icon="@drawable/ic_rate"
android:title="@string/rate" />
<item
android:id="@+id/aboutFragment"
android:icon="@drawable/ic_about_me"
android:title="@string/about"/>
</group>
</menu>
MainActivity.kt (setupMenus() and setupView() are called in onCreate)
override fun setupView(){
val host: NavHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment? ?: return
val navController = host.navController
val drawerLayout : DrawerLayout? = findViewById(R.id.drawer_layout)
appBarConfiguration = AppBarConfiguration(
setOf(R.id.bridgeFragment, R.id.categoryFragment, R.id.favoriteFragment, R.id.aboutFragment),
drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
setupNavigationMenu(navController)
navController.addOnDestinationChangedListener { _, _, _ ->
if (drawer_layout.isDrawerOpen(GravityCompat.START))
drawer_layout.closeDrawer(GravityCompat.START)
}
}
fun updateHasMenu(hasMenu: Boolean) { this.hasMenu = hasMenu }
private fun setupNavigationMenu(navController: NavController) {
val sideNavView = findViewById<NavigationView>(R.id.nav_view)
sideNavView?.setupWithNavController(navController)
}
override fun onSupportNavigateUp(): Boolean {
return findNavController(R.id.nav_host_fragment).navigateUp(appBarConfiguration)
}
override fun setupMenus(){
setSupportActionBar(toolbar)
supportActionBar?.let {
it.setDisplayHomeAsUpEnabled(true)
it.setHomeAsUpIndicator(R.drawable.ic_menu)
}
}
That's the whole working code for your first,secod and third question.
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
private lateinit var navController: NavController
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
navController = findNavController(R.id.nav_controller_fragment)
appBarConfiguration = AppBarConfiguration(navController.graph,drawer_layout)
setupActionBarWithNavController(navController,appBarConfiguration)
drawer()
setupNavigationMenu()
nav_view.setNavigationItemSelectedListener(this)
}
private fun drawer() {
drawer_layout.addDrawerListener(object :DrawerLayout.DrawerListener{
override fun onDrawerStateChanged(newState: Int) {
}
override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
}
override fun onDrawerClosed(drawerView: View) {
}
override fun onDrawerOpened(drawerView: View) {
}
})
}
private fun setupNavigationMenu() {
nav_view.let {
NavigationUI.setupWithNavController(it, navController)
}
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_settings -> true
else -> NavigationUI.onNavDestinationSelected(item,navController) || super.onOptionsItemSelected(item)
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
drawer_layout.closeDrawer(GravityCompat.START)
return when (item.itemId) {
R.id.nav_gallery -> true
else ->
NavigationUI.onNavDestinationSelected(
item,navController) || super.onOptionsItemSelected(item)
}
}
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
}
And that's the whole project link and apk.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With