jetpack navigation version 2.4.0-rc01 has issue with custom fragment factory

Viewed 351

In navigation component version 2.3.5 everything works fine but in version 2.4.0-rc01 I have some weird issues:

  1. I have a destination change listener, in the back button click fragment is popped back but the destination listener is not called
  2. When getting back, for example from Cfragment to Bfragment I print the current destination id it shows that the current destination is Cfragment but in UI app is on Bfragmnet

with downgrading to version 2.3.5 or using default navHostFragment all issues are solved.

fragment factory class:

class MyFragmentFactory @Inject constructor(

) : FragmentFactory() {

    override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
        return when (className) {
            AFragment::class.java.name -> {
                AFragment()
            }
            BFragment::class.java.name -> {
                BFragment()
            }
            CFragment::class.java.name -> {
                CFragment()
            }
            FirstFragment::class.java.name -> {
                FirstFragment()
            }
            else -> super.instantiate(classLoader, className)
        }
    }
}

custom nav host fragment:

@AndroidEntryPoint
class MyNavHostFragment : NavHostFragment() {


    @Inject
    lateinit var fragmentFactory: MyFragmentFactory

    override fun onAttach(context: Context) {
        super.onAttach(context)
        childFragmentManager.fragmentFactory = fragmentFactory
    }

}

this is my full source code with different versions of the navigation component in various branches and the main branch is a buggy one

1 Answers

After submitting this bug in the google issue tracker they said that this bug is fixed internally in navigation component version 2.4.1 and 2.5.0-alpha02

Related