Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Navigation Graph With NavigationView in it

I have the scenario like I have one MainActivity which contains (login/signup fragments). And I have one HomeActivity(which contains navigationview with drawer layout in it.)

and both two activities are having their separate navigation_graph. When I include home_navigation_graph in main_navigation_graph and navigate to it the DrawerLayout doesn't work. But when I do it separately like withou nesting only calling the home_navigation_graph(HomeActivity) as launcher everything works fine. Here is my sample code:

<navigation 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/nav_graph_login"
    app:startDestination="@id/loginFragment">

    <fragment
        android:id="@+id/loginFragment"
        android:name="com.example.app.ui.login.LoginFragment"
        android:label="LoginFragment"
        tools:layout="@layout/fragment_login">
        <action
            android:id="@+id/action_loginFragment_to_signupFragment"
            app:destination="@id/signupFragment" />
        <action
            android:id="@+id/action_loginFragment_to_mobile_navigation"
            app:destination="@id/mobile_navigation"
            app:launchSingleTop="true"
            app:popUpTo="@+id/nav_graph_login"
            app:popUpToInclusive="true" />
    </fragment>

    <fragment
        android:id="@+id/signupFragment"
        android:name="com.example.app.ui.signup.SignupFragment"
        android:label="SignupFragment"
        tools:layout="@layout/fragment_signup">
        <action
            android:id="@+id/action_signupFragment_to_loginFragment"
            app:destination="@id/loginFragment"
            app:launchSingleTop="true"
            app:popUpTo="@id/signupFragment"
            app:popUpToInclusive="true" />
    </fragment>

    <include app:graph="@navigation/mobile_navigation" />
</navigation>

when I call the second mobile navigation graph at login button click(using code belwo) the HomeActivity opens but with only homeDestinationFragment is visble rest nothing works

findNavController().navigate(R.id.action_loginFragment_to_mobile_navigation)

and below the code is for home_navigation_graph.xml

<navigation 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/mobile_navigation"
    app:startDestination="@+id/nav_home">

    <fragment
        android:id="@+id/nav_home"
        android:name="com.example.app.ui.home.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/nav_gallery"
        android:name="com.example.app.ui.earnings.VendorEarningFragment"
        android:label="@string/menu_vendor_earning"
        tools:layout="@layout/fragment_vendor_earning" />

    <fragment
        android:id="@+id/nav_slideshow"
        android:name="com.example.app.ui.slideshow.SlideshowFragment"
        android:label="@string/menu_slideshow"
        tools:layout="@layout/fragment_slideshow" />

    <fragment
        android:id="@+id/nav_tools"
        android:name="com.example.app.ui.tools.ToolsFragment"
        android:label="@string/menu_tools"
        tools:layout="@layout/fragment_tools" />

    <fragment
        android:id="@+id/nav_share"
        android:name="com.example.app.ui.share.ShareFragment"
        android:label="@string/menu_share"
        tools:layout="@layout/fragment_share" />

    <fragment
        android:id="@+id/nav_send"
        android:name="com.example.app.ui.send.SendFragment"
        android:label="@string/menu_send"
        tools:layout="@layout/fragment_send" />
</navigation>

Please let me know what I'm missing. I'm stuck at this for weeks.

like image 629
Kotdroid Avatar asked Aug 06 '26 09:08

Kotdroid


1 Answers

In your home_navigation_graph just include containing the drawer layout and navigation menu instead of whole graph. And it worked fine for me you can just check out my github repo.

replace below lines

 <include app:graph="@navigation/mobile_navigation" />

with this code snippet below

<activity
    android:id="@+id/mainActivity"
    android:name="com.example.nav_experiment.MainActivity"
    android:label="activity_main"
    tools:layout="@layout/activity_main" />

In below Git link there is Navigation Drawer which get open on Login Button Click, As per Query asked

For Detail Description Follow git Link -> https://github.com/gautammittal23/NaV_Graph_With_login_navigation_drawer

like image 109
Gautam Mittal Avatar answered Aug 07 '26 23:08

Gautam Mittal