'calling findNavController().popBackStack() does not resume previous fragment on stack

I just set up a projet with Android navigation component and here's the structure of my graph:

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


    <fragment
        android:id="@+id/a"
        android:name="com.example.tutorial.fragmentA"
        android:label="a">

        <action android:id="@+id/action_a_to_b"
            app:destination="@id/b"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim"
            app:popEnterAnim="@anim/nav_default_pop_enter_anim"
            app:popExitAnim="@anim/nav_default_pop_exit_anim"/>

    </fragment>

    <fragment
        android:id="@+id/b"
        android:name="com.example.tutorial.fragmentB"
        android:label="b">

    </fragment>

</navigation>

In fragment A I navigate to B like this:

 findNavController().navigate(R.id.action_a_to_b)

In fragment B, I have a custom toolbar and the idea is that a click on the top left arrow should close fragment B and resume A:

(activity as AppCompatActivity).setSupportActionBar(binding.toolbar)

 binding.toolbar.setNavigationOnClickListener {
        findNavController().popBackStack()
    }

Same goes if I click on the Key down press button:

       requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner){
        findNavController().popBackStack()
    }

The problem I'm still stuck in fragment B: the exit animation starts and ends and I'm still in fragment B. Any ideas how to go about to fix this ?



Solution 1:[1]

I've created a small project with 2 fragments and it works flowless. We have a login Screen and a screen with passwords.

LoginFragment

class LoginFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_login, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val toolbar = view.findViewById<androidx.appcompat.widget.Toolbar>(R.id.loginToolbar)
        toolbar.navigationIcon = null
        // Set the Toolbar as your activity's ActionBar
        (requireActivity() as AppCompatActivity).setSupportActionBar(toolbar)

        // Find this Fragment's NavController
        val navController = NavHostFragment.findNavController(this);

        // And set up the ActionBar
        NavigationUI.setupActionBarWithNavController(
            requireActivity() as AppCompatActivity,
            navController
        )

        val b = view.findViewById<MaterialButton>(R.id.loginButton)
        b.setOnClickListener {
            this.findNavController().navigate(R.id.passwordsFragment)
        }
    }
}

PasswordFragment

class PasswordsFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_passwords, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val toolbar = view.findViewById<androidx.appcompat.widget.Toolbar>(R.id.myToolbar)
        // Set the Toolbar as your activity's ActionBar
        (requireActivity() as AppCompatActivity).setSupportActionBar(toolbar)

        // Find this Fragment's NavController
        val navController = NavHostFragment.findNavController(this);

        // And set up the ActionBar
        NavigationUI.setupActionBarWithNavController(
            requireActivity() as AppCompatActivity,
            navController
        )
        toolbar.setNavigationOnClickListener {
            navController.navigateUp()
        }
    }
}

Toolbar in the LoginFragment XML

  <androidx.appcompat.widget.Toolbar
        android:id="@+id/loginToolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black"
        app:layout_constraintTop_toTopOf="parent" />

Toolbar in the PasswordFragment XML

<androidx.appcompat.widget.Toolbar
    android:id="@+id/myToolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/black"
    app:layout_constraintTop_toTopOf="parent" />

MainActivity XML

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/constraintLayout3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="6dp"
    android:fillViewport="true"
    android:orientation="vertical">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/my_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>

I navigate from the loginFragment to the PasswordFragment and then I press the up button and I get to the previous fragment :)

Let me know if it works

Solution 2:[2]

private lateinit var appBarConfiguration: AppBarConfiguration
//onCreate
val navHostFragment =
        supportFragmentManager.findFragmentById(R.id.fragment4) as NavHostFragment
    val navController = navHostFragment.navController
    appBarConfiguration = AppBarConfiguration(navController.graph, drawerLayout)
    setupActionBarWithNavController(navController, appBarConfiguration)
    navDrawView.setupWithNavController(navController)
    bottomAppBar.background = null
    bottomAppBar.setupWithNavController(navController)
//automatic NavigateUp setup
override fun onSupportNavigateUp(): Boolean {
    val navController = findNavController(R.id.fragment4)
    return navController.navigateUp(appBarConfiguration)
            || super.onSupportNavigateUp()
}

Can you try this setup in your main activity, it's worked for me I use bottomNav navdrawer and appbar together.

if you wanna use custom Toolbar

//in fragment or activity
private fun handleToolbarChanges() {
    with(binding.toolbarDataAnalysis) {
        setNavigationOnClickListener { onBackPressed() }
        title = "your title here"
    }
}
//in xml file
<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbarDataAnalysis"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/green"
    app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
    app:titleTextColor="@color/white" />

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2