Search code examples
androidandroid-fragmentskotlinandroid-toolbarnavigation-drawer

Android 'back' action on Toolbar with Navigation Drawer


Please help with this, I've been going around in circles for hours! I have a navigation drawer set up and a toolbar (see code below) I CAN NOT get the back/home function to work, as clicking it results in opening the drawer.

This is in my MainActivity, called during 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()
}

The following is within the OnCreateView method of my fragments...

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

The navigation drawer works fine. Changing the title of my toolbar works fine. The hamburger icon is changed to a back arrow within the fragment just fine... However, each time I press the back arrow, it opens the drawer... kind of as if a listener for the drawer is above the back arrow...

Implementing any code within OnOptionsItemSelected results in nothing as no call is made to that method, since it just opens the drawer.

Would really appreciate someone to point out the obvious in what I have done wrong.

Thanks.

EDIT main_activity.xml as requested to view...

<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" />


Solution

  • Did you try to use something like this to set it like back button?

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

    And override your onBackPressed

    @Override
    public void onBackPressed() {
        if (sendBackPressToDrawer()) {
            return;
        }
    
        if (sendBackPressToFragmentOnTop()) {
            return;
        }
        super.onBackPressed();
        if (fragmentManager.getBackStackEntryCount() > 0) {
            return;
        }
        finish();
    }