Search code examples
androidkotlinandroid-architecture-navigation

Pressing back button exits the app instead of navigating to the previous screen while using navigation component


I am using the navigation architecture component in my app and whenever I navigate to a screen and then try to return to the previous screen using the back button, instead of navigating back the app exits.

My Navigation Graph looks like this:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_nav_host_fragment"
    app:startDestination="@id/authFragment">

    <fragment
        tools:layout="@layout/fragment_main_screen"
        android:id="@+id/sellerListScreenFragment"
        android:name="com.example.paylater.ui.SellerMainScreenFragment"
        android:label="SellerListScreenFragment" >
        <action
            android:id="@+id/action_sellerListScreenFragment_to_creditFragment"
            app:destination="@id/credit_nav_graph" />
    </fragment>
    <fragment
        android:id="@+id/authFragment"
        android:name="com.example.paylater.ui.AuthFragment"
        android:label="AuthFragment"
        tools:layout="@layout/fragment_auth" >
        <action
            android:id="@+id/action_authFragment_to_sellerListScreenFragment"
            app:destination="@id/sellerListScreenFragment" />
    </fragment>

    <navigation android:id="@+id/credit_nav_graph"
        app:startDestination="@id/creditFragment">
        <fragment
            android:id="@+id/creditAddFragment"
            android:name="com.example.paylater.ui.CreditAddFragment"
            android:label="CreditAddFragment"
            tools:layout="@layout/fragment_credit_add">
            <action
                android:id="@+id/action_creditAddFragment_self"
                app:destination="@id/creditAddFragment" />
            <action
                android:id="@+id/action_creditAddFragment_to_transactionResultFragment"
                app:destination="@id/transactionResultFragment" />
        </fragment>
        <fragment
            android:id="@+id/transactionResultFragment"
            android:name="com.example.paylater.ui.TransactionResultFragment"
            android:label="TransactionResultFragment"
            tools:layout="@layout/fragment_transaction_result">
            <action
                android:id="@+id/action_transactionResultFragment_self"
                app:destination="@id/transactionResultFragment" />
        </fragment>
        <fragment
            android:id="@+id/creditFragment"
            android:name="com.example.paylater.ui.CreditFragment"
            android:label="CreditFragment"
            tools:layout="@layout/fragment_credit_enter_no">
            <action
                android:id="@+id/action_creditFragment_to_creditAddFragment"
                app:destination="@id/creditAddFragment" />
            <action
                android:id="@+id/action_creditFragment_self"
                app:destination="@id/creditFragment" />
        </fragment>
    </navigation>
    <action android:id="@+id/action_global_sellerListScreenFragment" app:destination="@id/sellerListScreenFragment"/>
</navigation>

I've tried to fix this using a custom onBackPressedDispatcher callback, but on top of leading to some strange behaviour it is also tedious to do it for every fragment and seems to be too much work for something that should be handled by the navigation component.

So is there a way to fix this problem as I have gone through my code but there doesn't seem to be anything that influences the navigation behaviour yet it doesn't work as intended.

Thanks for helping out!

Edit :-

As asked, I am providing the setup using which I navigate from , for example, SellerMainScreenFragment to CreditFragment

binding.fabCredit.setOnClickListener {        
    findNavController().
        navigate(R.id.action_sellerListScreenFragment_to_creditFragment)
}

Here I use a FAB to navigate from SellerMainScreenFragment to CreditFragment.

But after navigating to CreditFragment, on pressing the back button the app just exits instead of navigating back.


Solution

  • I think you forgot to add app:defaultNavHost="true" to the NavHostFragment in your main Activity layout:

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />
    

    From the documentation:

    The app:defaultNavHost="true" attribute ensures that your NavHostFragment intercepts the system Back button. Note that only one NavHost can be the default. If you have multiple hosts in the same layout (two-pane layouts, for example), be sure to specify only one default NavHost.

    https://developer.android.com/guide/navigation/navigation-getting-started