ActionBar Back Button to Navigation Button

Ferron Nijland 121 Reputation points
2021-03-26T10:30:35.847+00:00

Hello, I'm using the default drawer template from visual studio for my Android App. When I set the drawer hamburger Icon to show as a back arrow: SupportActionBar.SetDisplayUseLogoEnabled(false); SupportActionBar.SetDisplayHomeAsUpEnabled(true); I get the back arrow: 81903-image.png][1] When I click on it the drawer opens instead going back to the previous backstack item. I'm using AndroidX.AppCompat.App.ActionBar It has been setup like this: toolbar = baseView.FindViewById<Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar); DrawerLayout drawer = baseView.FindViewById<DrawerLayout>(Resource.Id.drawer_layout); var toggle = new ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close); drawer.AddDrawerListener(toggle); toggle.SyncState(); NavigationView navigationView = baseView.FindViewById<NavigationView>(Resource.Id.nav_view); navigationView.SetNavigationItemSelectedListener(this); I've read some blogs about you have to catch the OnOptionsItemSelected event and then handle this yourself. But this doesn't get fired when I click the back arrow. app_bar.xml: <?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="[@/AppTheme.AppBarOverlay"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@STYLE /AppTheme.PopupOverlay" /> </com.google.android.material.appbar.AppBarLayout> <include layout="@layout/base_content" /> <!-- <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@Krak ruiz :drawable/ic_dialog_email" />--> </androidx.coordinatorlayout.widget.CoordinatorLayout> [1]: /api/attachments/81903-image.png?platform=QnA

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,363 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,961 Reputation points
    2021-03-26T13:38:17.42+00:00

    've read some blogs about you have to catch the OnOptionsItemSelected event ... But this doesn't get fired when I click the back arrow.

    The OnOptionsItemSelected event is to detect the toolbar of the Activity. But the default drawer template from VS place the toolbar inside the DrawerLayout. To fix this, please move the Toolbar outside the DrawerLayout las below.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.appcompat.widget.Toolbar  
            android:id="@+id/toolbar"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:minHeight="?attr/actionBarSize"  
            android:background="#33B86C"  
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"  
            app:layout_scrollFlags="scroll|enterAlways" />  
    
        <androidx.drawerlayout.widget.DrawerLayout
            android:id="@+id/drawer_layout"  
            android:layout_height="match_parent"  
            android:fitsSystemWindows="true"  
            android:layout_width="match_parent">  
            <RelativeLayout  
                android:orientation="vertical"  
                android:layout_width="match_parent"  
                android:layout_height="match_parent" />  
            <com.google.android.material.navigation.NavigationView
                android:id="@+id/nav_view"  
                android:layout_height="match_parent"  
                android:layout_width="200dp"  
                android:layout_gravity="start"  
                android:fitsSystemWindows="true"  
                app:menu="@menu/_menu" />  
        </androidx.drawerlayout.widget.DrawerLayout>  
    </LinearLayout>
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.