Search code examples
androidnavigationnavigation-drawerandroidxnavigationcontroller

Android Navigation Drawer Template Fails to Work Unless it is navigated by tab and cursor keys on Emulator AS4.0


Android Studio 4. New Project Navigation Drawer Activity. Add name etc and then build. Send to Nexus 6P API 28 Emulation, Pixel 3a API29 Emulation. Also to Samsung SM-J530Y Android 9 and Nokia 7Plus Android 10.

All instances allow the drawer to be opened and all instances do not allow the drawers to be selected but attempting to select an entry closes the drawer.

I have added an onDestinationChangedListener to the navController and this only kicks out the initial home selection event.

Tried adding navView.setNavigationItemSelectedListener and this is never triggered

Searching only drags up old issues but nothing using the newer NavController. Tried updating to the latest beta01 kotlin dependencies

def navVersion = "2.3.0-beta01"
implementation "androidx.navigation:navigation-fragment-ktx:$navVersion"
implementation "androidx.navigation:navigation-ui-ktx:$navVersion"

from the template "2.2.2"

Added also the apply plugin: 'androidx.navigation.safeargs.kotlin' to no avail.

Would pull out some hair but I do not have much left.

I am wanting to add drawer navigation away from tabs in my pager app hence the interest in getting this to work.


Solution

  • As highlighted by Mike M in the comments the NavigationView in the activity_main.xml was not in the correct order.

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout 
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    
    <com.google.android.material.navigation.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_main"
        app:menu="@menu/activity_main_drawer" />
    
    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </androidx.drawerlayout.widget.DrawerLayout>
    

    is wrong.

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout 
    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:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
    
    
    
        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <com.google.android.material.navigation.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_main"
            app:menu="@menu/activity_main_drawer" />
    </androidx.drawerlayout.widget.DrawerLayout>
    

    Works. So simple yet so very frustrating.