Issue
I'm new at navigation, my start destination in navigation component is not the same with my top destination so I used AppBarConfiguration to use a custom top destination but when I want to set NavigationView with navController using NavigationUI.setupWithNavController(binding.navView, navController) for setting the navigation component with drawer Items, but the app do not recognize my custom top destination when I press an Item in navigation drawer, navigate to a different fragment and press back button to navigate back to the top destination. It shows me the start destination but I want to see the custom top destination
this is them main activity code:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.NavigationUI
import com.example.android.pass.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var drawerLayout: DrawerLayout
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityMainBinding
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setTheme(R.style.AppTheme)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val toolbar= binding.toolbar
drawerLayout = binding.drawerLayout
navController = this.findNavController(R.id.myNavHostFragment)
setSupportActionBar(toolbar)
navController
.addOnDestinationChangedListener { nc: NavController, nd: NavDestination, args: Bundle? ->
if (nd.id == R.id.accountListFragment) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
} else {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
}
}
val topLevelDestinations = setOf(R.id.accountListFragment)
appBarConfiguration = AppBarConfiguration.Builder(topLevelDestinations)
.setDrawerLayout(drawerLayout)
.build()
NavigationUI.setupWithNavController(binding.navView, navController)
NavigationUI.setupWithNavController(toolbar,navController,appBarConfiguration)
}
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(navController,appBarConfiguration)
}
}
and this is my drawer menu with two Items with the same id with the fragments I want to navigate:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="@+id/aboutFragment"
android:icon="@drawable/question"
android:title="About" />
<item
android:id="@+id/settingsFragment"
android:icon="@drawable/settings"
android:title="settings" />
</menu>
Solution
I found the answer, instead of using the following code to set navigationView with navController:
NavigationUI.setupWithNavController(binding.navView, navController)
I used following code in the main activity:
binding.navView.setNavigationItemSelectedListener(this)
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.about_item -> navController.navigate(R.id.aboutFramgent)
R.id.settings_item -> navController.navigate(R.id.settingsFramgent)
}
return true
}
Answered By - Amin Hosseini
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.