Search code examples
androidandroid-fragmentskotlinandroid-navigationandroid-architecture-navigation

How to handle navigation properly


I have one question, what should I use to navigate from 1 Activity that hosts multiple fragments.

The goal is 1 Activity that hosts multiple fragments.

I'm using the Navigation Components Architecture

My goal is to know which is the best way to implement the navigation

The currently implemented way of doing navigation is this

class MainMenuActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_menu)
    }

    override fun onSupportNavigateUp() = findNavController(R.id.nav_host_fragment).navigateUp()
}

Then to navigate between Fragments after inflating the default one is this (From Fragment A to Fragment B

Fragment A : Fragment() {

onViewCreated(...){
 btn.setOnClickListener{
 findNavController.navigate(R.id.nextAction)
}

From Fragment B to Fragment C

Fragment B : Fragment() {

    onViewCreated(...){
     btn.setOnClickListener{
     findNavController.navigate(R.id.nextAction)
    }

My question is, is it a good practice navigating between fragments this way ? Because I feel like Im doing a navigation between fragments but without caring about the Main container Activity.

What I'm thinking to do is something like this

class MainMenuActivity : BaseActivity() {

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main_menu)
        }

        override fun onSupportNavigateUp() = findNavController(R.id.nav_host_fragment).navigateUp()

        fun navigateToFragment(id:Int){
          findNavController.navigate(id)
        }

    }

And then call this from each fragment to go to a desired destination

 Fragment A : Fragment() {

    onViewCreated(...){
     btn.setOnClickListener{
     requireActivity().navigateToFragment(R.id.nextAction)
    }

Is this better to have 1 activity that hosts a stack of Fragments, or its better the first way ?

Doing it the first way I think Im hosting fragments within fragments, making me do childFragmentManager to get the fragment manager of those fragments.

And also makes it harder to extend some methods from the activity itself.

Thanks


Solution

  • First of all, you are doing the same thing in both methods. Calling NavigationController from fragment, activity or any other view if that matters will return you the same NavigationController.

    Second of all, the point of Navigation Component is to split navigation from its containing Activity. In fact the direct parent of all your fragments are the NavHostFragment that you have defined in your xml. So, activity has nothing to do with navigating between fragments.

    Third, regardless of doing "first way" or "second way" (technically they are same thing as I mentioned in my first point) while navigating it does not mean that you are hosting fragments within fragments. Instead Navigation Component will replace your container with new fragment every time you visit new destination.

    And finally, it's better to stick with what the developers suggested. Try reading the documentation and you don't see anywhere where they change destination through Activity.