How to close current fragment in Android while using Navigation?

Viewed 1936

after setting listener on button I create navigationOnClickListener, which set startDestination to current fragment, after .navigate() it changes the destination to another fragment, but the view is not changed

onClickListener

 navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment)
 view.findViewById<Button>(R.id.fragment_button).setOnClickListener {
    navController.navigate(R.id.Camera2VideoFragment_To_FirstFragment)
 }

closeFragment()

private fun closeFragment()
{
    closePreviewSession()
    closeCamera()
    parentFragmentManager.popBackStack()
    updatePreview()
}

main.xml (navigation graph)

    <?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.xml"
    app:startDestination="@id/cameraFragment"
    >

    <fragment
        android:id="@+id/cameraFragment"
        android:name="com.example.camera2videotextureview.Camera2VideoFragment"
        android:label="Camera2VideoFragment"
        tools:layout="@layout/fragment_camera2_video">
        <action
            android:id="@+id/navigateToSecondFragment"
            app:destination="@id/firstFragment" />
    </fragment>

    <fragment
        android:id="@+id/firstFragment"
        android:name="com.example.camera2videotextureview.FirstFragment"
        android:label="FirstFragment"
        tools:layout="@layout/fragment_first" />
    
</navigation>

Main fragment is the camera fragment using Camera2 API. FirstFragment is just blank kotlin class where layout contains just one TextView.

EDIT: Now I have edited onClickListener, I am not able to change the current view, I need to use popBackStack on current fragment, but donot know how.

2 Answers

Navigation.findNavController(mActivity, R.id.nav_host_fragment).popBackStack();

I resolved my issue, which was that I have tried to implemented 3 different styles of Navigation. The whole navigation has changed in the past 4 years and I also found out that I have mixed up different versions from lower APIs with higher APIs.

I am not using closeFragment() method anymore.

Following declaration in code and the code itself has to be implemented in MainActivity:

 navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment)
 view.findViewById<Button>(R.id.fragment_button).setOnClickListener {
    navController.navigate(R.id.Camera2VideoFragment_To_FirstFragment)
 }

Also you have to setup correctly nav_host_fragment in xml file for MainActivity:

<androidx.fragment.app.FragmentContainerView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:defaultNavHost="true"
    app:navGraph="@navigation/main"
    />
Related