Android 'back' 工具栏上的带导航抽屉的操作

Android 'back' action on Toolbar with Navigation Drawer

请帮忙解决这个问题,我已经兜圈子好几个小时了! 我有一个导航抽屉设置和一个工具栏(见下面的代码) 我无法使用 back/home 功能,因为单击它会打开抽屉。

这是在我的 MainActivity 中,在 OnCreate 期间调用。

 private fun initialiseViews() {

    // Initialise the action bar as the XML toolbar, and set the Title as Dashboard
    setSupportActionBar(toolbar)
    supportActionBar!!.title = "Dashboard"

    // Set-up the NavigationView and its listener
    nav_view.setNavigationItemSelectedListener(this)

    // Sets the hamburger toggle and its actions for the toolbar
    val toggle = ActionBarDrawerToggle(
            this,           //host activity
            drawer_layout,        //Drawer Layout object
            toolbar,              // Toolbar Object
            R.string.nav_open,    //description for accessibility
            R.string.nav_closed   //description for accessibility
    )

    // Set the sync-state and draw-listener to the toggle (hamburger)
    drawer_layout.addDrawerListener(toggle)
    toggle.syncState()
}

以下是我的片段的 OnCreateView 方法中...

(activity as AppCompatActivity).supportActionBar!!.title = "About"
(activity as MainActivity).supportActionBar!!.setHomeButtonEnabled(true)
(activity as MainActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(true)

导航抽屉工作正常。更改工具栏的标题效果很好。汉堡包图标在片段内更改为后退箭头就好了...... 然而,每次我按下后退箭头,它都会打开抽屉...有点像抽屉的监听器在后退箭头上方...

在 OnOptionsItemSelected 中执行任何代码都不会产生任何结果,因为不会调用该方法,因为它只是打开抽屉。

非常感谢有人指出我做错的明显之处。

谢谢。

编辑 main_activity.xml 按要求查看...

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@drawable/background_graphic">

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="@color/colorLightOrange"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
        app:menu="@menu/toolbar_list_menu"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/not needed for post" />

    <android.support.v4.view.ViewPager
        android:id="@+id/not needed for post" />

</FrameLayout>

<!-- Navigation Drawer -->
<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/navigation_menu" />

您是否尝试使用类似的东西将其设置为后退按钮?

        (activity as MainActivity).supportActionBar!!.setNavigationIcon(R.drawable.back)
        (activity as MainActivity).supportActionBar!!.setNavigationOnClickListener {
            (activity as MainActivity).supportActionBar!!.onBackPressed()
        }

并覆盖你的onBackPressed

@Override
public void onBackPressed() {
    if (sendBackPressToDrawer()) {
        return;
    }

    if (sendBackPressToFragmentOnTop()) {
        return;
    }
    super.onBackPressed();
    if (fragmentManager.getBackStackEntryCount() > 0) {
        return;
    }
    finish();
}