Two NavHosts with their own NavControllers and NavGraphs (Navigation Component) //Android Kotlin Dev Diary

I am trying to make a single-activity app because I’d like to easily share data across screen using, ideally one, or as few as possible ViewModels.

NavGraph 1

Host: Layout file of MainActivity

Destination(s): CreateLogFragment

NavGraph 2

Host: Layout file of CreateLogFragment

Destination(s): CreateLogFoodFragment, CreateLogNotesFragment

(CreateLogFragment has a BottomNavigationBar that switches between CreateLogFoodFragment and CreateLogNotesFragment)

The problem was that NavGraph 2 was not getting the correct NavHost and NavController in the CreateLogFragment class.

And the solution is to retrieve the NavHost in CreateLogFragment like so:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

I’d like to write a full tutorial on how to do this but I’m not sure if I could spare some time. If you’ve been reading my recent posts you’ll notice that I’m not able to put it so much effort in explaining everything. These days I’m always in a hurry to finish stuff. But I’ll still try!!

Related Posts:

Posts that may be related to "Two NavHosts with their own NavControllers and NavGraphs (Navigation Component) //Android Kotlin Dev Diary":

Catzie

An odd human being who happens to have a variety of ever-changing interests, but right now they are programming, making up silly song/rap lyrics, K-pop, drawing, creating unique dessert/drink flavors, obsessing about finding out how some things works, automation, anime, video games... Ran online dessert shops Cookies PH and Catzie's Cakery in her past life.

Leave a Reply

Your email address will not be published. Required fields are marked *