Search code examples
androidandroid-architecture-navigation

Navigation Component currentDestination null after pressing back-button


I've implemented a navigation flow using the Navigation Component (v2.3.2). When I test the flow for the first time everything works fine. But when I press the back button to go back to the home fragment and try to test the flow again, the app crashes because the currentDestination is null. The Navigation Component seems to get stuck when I press the back-button.

The error:

java.lang.IllegalArgumentException: Navigation action/destination com.tridie2000.myapp:id/action_fragmentA_to_fragmentB cannot be found from the current destination Destination(com.tridie2000.myapp:id/fragmentB) label=b_fragment class=com.tridie2000.myapp.presentation.fragments.BFragment

This is how I configure the Navigation Component in my MainActivity:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setupBottomNavBar()
        setupNavigationUI()
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        val inflater: MenuInflater = menuInflater
        inflater.inflate(R.menu.main_menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item)
    }

    private fun setupBottomNavBar() {
        val navController = findNavController(R.id.nav_host_fragment)
        val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_navigation)
        bottomNav.setupWithNavController(navController)
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        return navController.navigateUp()
    }

    private fun setupNavigationUI() {
        val navController = findNavController(R.id.nav_host_fragment)
        setupActionBarWithNavController(navController)
    }
}

This is the layout code of the MainActivity:

<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".presentation.activities.MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/themeColor"
        app:itemIconTint="@drawable/my_nav_bar_item_color"
        app:itemTextColor="@drawable/my_nav_bar_item_color"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/main_tabbar_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph"
        tools:ignore="FragmentTagUsage" />

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • Ok, after some more digging I discovered why my app was crashing. In earlier versions of the Navigation Component the error was:

    Action is unknown to this NavController

    The new error (as of v2.3) is:

    Action/destination cannot be found from the current destination

    This gets triggered when you execute the findNavController().navigate(action) multiple times after each other.

    I am using RxJava and my observer got triggered multiple times causing this error.

    After fixing my RxJava implementation my navigation flow is working fine.