Search code examples
androidxmlkotlinnavigationandroid-architecture-navigation

Why I cannot go back to previous fragment with Navigation Component?


In my app I have 3 main screens - Explore, My Groups, Profile. In My Groups I have list of groups and when I click on one I am redirected do single GroupFragment. That's work fine. But when I want bo back by gesture (from right side of the phone to center) the app is going to background and the "desktop" is displayed. I want to go back from group to MyGroups fragment.

Edit: The only way i can go back to previous fragment (MyGroups) is to create onClickListener on some button in Group Fragment. Like that:

  backButton.setOnClickListener { findNavController().popBackStack() }

Why my code below is not working as it should with gesture back swipe?

nav_dashboard_menu:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_dashboard_menu"
    app:startDestination="@id/nav_explore">

    <include app:graph="@navigation/nav_explore" />
    <include app:graph="@navigation/nav_my_groups" />
    <include app:graph="@navigation/nav_profile" />

</navigation>

nav_my_groups:

<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_my_groups"
    app:startDestination="@id/myGroupsFragment">

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

    <fragment
        android:id="@+id/myGroupsFragment"
        android:name="com.wojciechkula.locals.presentation.mygroups.MyGroupsFragment"
        android:label="My groups"
        tools:layout="@layout/fragment_my_groups">
        <action
            android:id="@+id/openGroup"
            app:destination="@id/nav_group">
            <argument
                android:name="groupId"
                app:argType="string" />
            <argument
                android:name="groupName"
                app:argType="string" />
        </action>
    </fragment>

</navigation>

nav_group:

<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_group"
   app:startDestination="@id/groupFragment">

   <fragment
       android:id="@+id/groupFragment"
       android:name="com.wojciechkula.locals.presentation.group.GroupFragment"
       android:label="Group"
       tools:layout="@layout/fragment_group">
       <argument
           android:name="groupId"
           app:argType="string" />
       <argument
           android:name="groupName"
           app:argType="string" />
   </fragment>

</navigation>

And I'm going from MyGroups to Group with navController and Destination. MyGroupsNavigator:

internal class MyGroupsNavigator @Inject constructor() {

    fun openGroup(navController: NavController, id: String, name: String) {
        val destination = MyGroupsFragmentDirections.openGroup(id, name)
        navController.navigate(destination)
    }
}

Solution

  • You can use OnBackPressDispatcher also. But don't forget to remove the callback in the handleOnBackPress method. here