mitchtabian / Open-API-Android-App

Kotlin, MVI, Hilt, Retrofit2, Coroutines, Room Persistence, REST API, Token Authentication

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Navigating bug (Destination does not exist in nav controller)

mitchtabian opened this issue · comments

Starting at version nav components versions 2.1.0-alpha03 my code will crash the app by doing the following:

  1. Nav to BlogFragment
  2. Press back
  3. Nav to BlogFragment

The problem seems to be the childFragmentManager. If you pop from there it removes the graph along with the last fragment, which causes the "Destination not part of this NavGraph" exception when you then try to navigate forward again.

Solution branch: https://github.com/mitchtabian/Open-API-Android-App/tree/navigation-bug

Notes:

Just replace the OnBackPressed function in BottomNavController.

Simply getting a reference to the NavController

val navController = fragmentManager.findFragmentById(containerId)!!
            .findNavController()

Then popping if backstack size > 2 (One for start destination and one for the NavGraph)

navController.backStack.size > 2{
    navController.popBackStack()
}

Here are all the changes: https://gist.github.com/mitchtabian/3d4e58e56a5f07d9ff582f6c275f58a0

The only reason I can think of that would cause this is I was using the childFragmentManager incorrectly. I suppose you should only use childFM if you set a FragmentFactory to generate the fragments from? Like I did in Espresso course. Not 100% sure.

Tested up to version 2.3.0-alpha03.