Toolbars Back button not working when using with n

2019-08-17 07:13发布

Below is the code

I have defined all my top level destinations with AppbarConfiguraion.

class MainActivity : DaggerAppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    lateinit var navController: NavController
    lateinit var binding: MainActivityBinding
    lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        navController = findNavController(R.id.navigation)
        setSupportActionBar(binding.toolbar)
        supportActionBar!!.setDisplayShowHomeEnabled(true)

        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.homeFragment,
                R.id.bulletinsFragment,
                R.id.serviceFragment,
                R.id.paymentsFragment,
                R.id.feedBackFragment,
                R.id.formsFragment,
                R.id.surveysFragment
            ),
            drawer_layout)
        setupActionBarWithNavController(
            this, navController, appBarConfiguration
            )

        val toggle = ActionBarDrawerToggle(
            this,
            drawer_layout,
            binding.toolbar,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close
        )
        drawer_layout.addDrawerListener(toggle)
        toggle.syncState()
        sideNV.setNavigationItemSelectedListener(this)
    }

    override fun onBackPressed() {
        if ((drawer_layout as DrawerLayout).isDrawerOpen(GravityCompat.START)) {
            (drawer_layout as DrawerLayout).closeDrawer(GravityCompat.START)
        } else {
            findNavController(R.id.navigation).navigateUp()
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {

        when (item.itemId) {
            R.id.nav_home, R.id.nav_service, R.id.nav_payments, R.id.nav_forms, R.id.nav_bulletins,
            R.id.nav_surveys, R.id.nav_feedbacks -> {
                navController.navigate(item.itemId)
            }
        }
        (drawer_layout as DrawerLayout).closeDrawer(GravityCompat.START)
        return true
    }

    override fun onSupportNavigateUp() = findNavController(R.id.navigation).navigateUp()
}

When moving from the top level fragments to other fragments the back arrow is shown but when on clicking it opens the drawer itself. When on clicking the system back button it's working fine.

Manifest

<activity
        android:name=".home.MainActivity"
        android:label="@string/app_name"
        android:parentActivityName=".home.MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
</activity>

3条回答
放荡不羁爱自由
2楼-- · 2019-08-17 07:45
class MainActivity : DaggerAppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    lateinit var navController: NavController
    lateinit var binding: MainActivityBinding
    lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        navController = findNavController(R.id.navigation)
        setSupportActionBar(binding.toolbar)
        supportActionBar!!.setDisplayShowHomeEnabled(true)

        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.homeFragment,
                R.id.bulletinsFragment,
                R.id.serviceFragment,
                R.id.paymentsFragment,
                R.id.feedBackFragment,
                R.id.formsFragment,
                R.id.surveysFragment
            ),
            drawer_layout
        )
        setupActionBarWithNavController(
            this, navController, appBarConfiguration
        )

        val toggle = ActionBarDrawerToggle(
            this,
            drawer_layout,
            binding.toolbar,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close
        )
        drawer_layout.addDrawerListener(toggle)
        toggle.syncState()
        sideNV.setNavigationItemSelectedListener(this)
        toolbar?.setNavigationOnClickListener {
            findNavController(R.id.navigation).navigateUp(appBarConfiguration)
        }
    }

    override fun onBackPressed() {
        if ((drawer_layout as DrawerLayout).isDrawerOpen(GravityCompat.START)) {
            (drawer_layout as DrawerLayout).closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {

        when (item.itemId) {
            R.id.nav_home, R.id.nav_service, R.id.nav_payments, R.id.nav_forms, R.id.nav_bulletins,
            R.id.nav_surveys, R.id.nav_feedbacks -> {
                navController.navigate(item.itemId)
            }
        }
        (drawer_layout as DrawerLayout).closeDrawer(GravityCompat.START)
        return true
    }

    override fun onSupportNavigateUp() = findNavController(R.id.navigation).navigateUp()
}

i just changed my code and added toolbar NavigationOnClickListener and in that listener i just added navigateUp(appBarConfiguration).Also there is a slight change in the onBackPressed() as in my previous code it will not close the app even if we are at the last fragment in the stack

查看更多
姐就是有狂的资本
3楼-- · 2019-08-17 07:47

First, remove the following because the navigation will handle this for you

val toggle = ActionBarDrawerToggle(
        this,
        drawer_layout,
        binding.toolbar,
        R.string.navigation_drawer_open,
        R.string.navigation_drawer_close
    )
    drawer_layout.addDrawerListener(toggle)
    toggle.syncState()
    sideNV.setNavigationItemSelectedListener(this)
}

override fun onBackPressed() {
    if ((drawer_layout as DrawerLayout).isDrawerOpen(GravityCompat.START)) {
        (drawer_layout as DrawerLayout).closeDrawer(GravityCompat.START)
    } else {
        findNavController(R.id.navigation).navigateUp()
    }
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {

    when (item.itemId) {
        R.id.nav_home, R.id.nav_service, R.id.nav_payments, R.id.nav_forms, R.id.nav_bulletins,
        R.id.nav_surveys, R.id.nav_feedbacks -> {
            navController.navigate(item.itemId)
        }
    }
    (drawer_layout as DrawerLayout).closeDrawer(GravityCompat.START)
    return true
}
查看更多
相关推荐>>
4楼-- · 2019-08-17 07:54

Try this

  override fun onSupportNavigateUp(): Boolean {
    return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
查看更多
登录 后发表回答