Wednesday, 8 May 2019

How to close navigation DrawerLayout onBackPressed in Navigation Controls Fragment

I have created an Androidx project with Navigation pattern. Now I want to close drawerLayout onBackPressed in fragment. and also want to set click listener to drawer menu items to my custom menu there is no way I guess to override the onBackPressed in fragment I want to know how i can achieve this.

Here is my drawer.

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:id="@+id/drawerLayoutHome"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="350dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/colorPrimaryDark"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:itemIconTint="@color/white"
        app:itemTextColor="@color/white"
        app:menu="@menu/activity_main_drawer"/>

 </androidx.drawerlayout.widget.DrawerLayout>

I want to achieve this:

override fun onBackPressed() {
    if (drawerLayoutHome.isDrawerOpen(GravityCompat.END)) {
        drawerLayoutHome.closeDrawer(GravityCompat.END)
       } else {
            super.onBackPressed()
        }

    }

But this is for activity, how to achieve this in fragment?



from How to close navigation DrawerLayout onBackPressed in Navigation Controls Fragment

No comments:

Post a Comment