Search code examples
androidkotlinandroid-fragmentsandroid-framelayout

Why there is two way to navigate between fragment?


When I'm learning to navigate between fragment, I have found two ways to do that. One of them is using Navigation Host

Navigation.findNavController(view).navigate(destination)

and of them using FrameLayout

supportFragmentManager.beginTransaction().apply{
    replace(flFragment,destination)
    commit()
}

Is there any differences between two of them? If not, what are the differences and which one is better? Tq in advance.


Solution

  • SupportFragmentManager & FragmentManager are relatively old ways of navigating an user through different fragments. They were widely used before, but have some drawbacks like:

    • Hard backstack navigation
    • Problems with adding or removing fragments, etc.

    The Navigation component is part of Android Jetpack. It's a new approach and it aims to simplify and solve some of the problems related to the old one described above. Also, it is not restricted to work only with Fragments.