2

I use Jetpack Navigation for navigating between fragments and activity. So, I have a MainActivity with a FragmentContainerView in the layout. I can easily navigate from fragment to fragment/activity. But, I can't find a way how to navigate from one activity to another activity/fragment with navController.

For example, from fragment FA, I call navController.navigate() to activity A. Now, I want to navigate from activity A to activity B or fragment FB.

I already tried this:

val host = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = host.navController

But always got this error

null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment

Thanks!

3 Answers 3

7

A note from the navigation component guide

Note: The Navigation component is designed for apps that have one main activity with multiple fragment destinations. The main activity is associated with a navigation graph and contains a NavHostFragment that is responsible for swapping destinations as needed. In an app with multiple activity destinations, each activity has its own navigation graph.

You shouldn't use the navigation component to navigate from one activity to another. It is made to swap fragments on a Fragment container.

You can get navController inside activity using

val navHostFragment =
        supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController

You also have to add android:name="androidx.navigation.fragment.NavHostFragment" to your fragment container

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
/>
1
  • you can use <fragment> tag instead of <FragmentContainerView> and access navController in activity using Navigation.findNavController(this, R.id.fragmentContainerView) where this refers to activity and R.id.fragmentContainerView is the id of <fragment> tag in activity xml file.
    – Reza
    May 9, 2022 at 10:57
1

For java coder;

it is possible to get NavController instance this way

NavHostFragment navHostFragment =(NavHostFragment)getSupportFragmentManager()
                                     .findFragmentById(R.id.nav_host_fragment);

NavController navController = navHostFragment.getNavController();
0

If you use FragmentContainerView and ViewBinding, here is a cleaner way to do it:

YourActivity.kt

class YourActivity : Activity() {
    private lateinit var binding: YourActivityBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = YourActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navController = (binding.navHostFragment.getFragment<NavHostFragment>())
            .findNavController()
    }
}

your_activity.xml

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.