与导航抽屉一起使用时,工具栏的“后退”按钮不起作用

时间:2019-05-20 11:10:13

标签: android navigation android-jetpack

下面是代码

我已经使用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()
}

从顶层碎片移动到其他碎片时,会显示后退箭头,但单击时会打开抽屉本身。单击系统的后退按钮时,它工作正常。

清单

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

3 个答案:

答案 0 :(得分:0)

首先,删除以下内容,因为导航将为您处理

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
}

答案 1 :(得分:0)

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()
}

我刚刚更改了代码,并添加了工具栏NavigationOnClickListener,并且在该侦听器中添加了 navigateUp(appBarConfiguration)。此外, onBackPressed()略有变化在我之前的代码中,即使我们位于堆栈中的最后一个片段,它也不会关闭应用程序

答案 2 :(得分:0)

尝试

  override fun onSupportNavigateUp(): Boolean {
    return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}