kotlinandroid-layoutandroid-fragmentsnavigationback-stack

How can I control navigation and backStack from main activity


I have two activities A and B and Three fragments C,D and E.

I used intent to move from activity A to B and didn't finish activity A as I want to get back to it later.

Activity B contains a custom layout and a fragment container.

Fragment C is Start destination and navigation graph allows navigation as C->D->E and back stack in following order.

C->D //After first back Stack

C //After Second back Stack reached final element in back stack.

A //Activity B is finish and Activity A is resumed after pressing one more back icon image

On press back icon image view of custom layout in activity B xml.

As there is no other action bar in any Fragment(C, D, E) so I can only use back icon image view of activity B.

I want to know how I can achieve this functionality My activity B xml is as follows

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--android:id="@+id/container"-->
    <include layout="@layout/custom_tool_bar"
        android:id="@+id/toolbar_settings_activity"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_dashboard_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/settings_navigation"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar_settings_activity"

        />
</androidx.constraintlayout.widget.ConstraintLayout>

xml code for custom tool bar is given bellow

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/app_gradient_color_background">
    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/iv_back"
        android:layout_width="20dp"
        android:layout_height="26dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginEnd="1dp"
        android:contentDescription="@string/content_description"
        android:scaleType="fitXY"
        android:src="@drawable/ic_white_color_back_24dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />
    <LinearLayout
        android:id="@+id/customlayout_width"
        android:layout_width="wrap_content"
        android:layout_height="?attr/actionBarSize"
        app:layout_constraintStart_toEndOf="@+id/iv_back"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:paddingEnd="10dp"
        android:paddingTop="6dp"
        android:paddingStart="10dp"
        android:text="@string/custom_tool_bar_Name"
        android:textColor="@color/colorOffWhite"
        android:textSize="26sp"/>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

custom layout used as action bar

Activity B when navgraph start destination is set to C fragment

Navgraph to navigate from fragment C->D->E in Activity B


Solution

  • 1- First in your Activity B get the navController:

    val navHostFragment = binding.navHostDashboardFragment.getFragment<NavHostFragment>()
    val navController = navHostFragment.navController
    

    2- Then you can add a click listener to your back button:

    binding.toolbarSettingsActivity.ivBack.setOnClickListener {
        val isStackPopped = navController.popBackStack() // Navigate back to the last fragment
        if (!isStackPopped) finish() // If there is no fragments in the stack finish the activity and go back to activity A
    }