Issue
I'm new to both Android development as a whole and nav graphs in particular. I have a Home screen with a bottom navigation component (fragments 2-5) and 4 buttons that navigate to other fragments (fragments 6-9). To get this structure to work I struggled with the same error and it eventually stopped giving errors, although I'm not confident that I "fixed" the issue properly. This is backed by the fact that I'm getting the same error now that I'm attempting to wrap the activity with a navigation drawer component. Please help me resolve this error and structure my code correctly. The error occurs when I click a button on the home fragment (navigation drawer and bottom nav behave as expected, currently).
My main_navigation.xml (details omitted for brevity):
...
<!-- Nav graph for the 4 buttons on Home fragment-->
<include app:graph="@navigation/home_nav_graph" />
<!--start destination - also part of home_nav_graph-->
<fragment
android:id="@+id/fragment_home"
...
</fragment>
<fragment
android:id="@+id/fragment_2"
...
</fragment>
<fragment
android:id="@+id/fragment_3"
...
</fragment>
<fragment
android:id="@+id/fragment_4"
...
</fragment>
<fragment>
android:id="@+id/fragment_5"
...
</fragment>
<!--Navigation drawer-->
<fragment
android:id="@+id/fragment_10"
... />
<fragment
android:id="@+id/fragment_11"
... />
<fragment
android:id="@+id/fragment_12"
... />
</navigation>
My home_nav_graph.xml (responsible for navigating from home to the 4 subsequent fragments through the buttons):
<fragment
android:id="@+id/fragment_home"
... >
<action
android:id="@+id/action_home_to_fragment6"
app:destination="@id/fragment_6" />
<action
android:id="@+id/action_home_to_fragment7"
app:destination="@id/fragment_7" />
<action
android:id="@+id/action_home_to_fragment8"
app:destination="@id/fragment_8" />
<action
android:id="@+id/action_home_to_fragment9"
app:destination="@id/fragment_9" />
</fragment>
<fragment
android:id="@+id/fragment_6"
... >
<action
android:id="@+id/action_fragment6_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
<fragment
android:id="@+id/fragment_7"
... >
<action
android:id="@+id/action_fragment7_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
<fragment
android:id="@+id/fragment_8"
... >
<action
android:id="@+id/action_fragment8_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
<fragment
android:id="@+id/fragment_9"
... >
<action
android:id="@+id/action_fragment9_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
</navigation>
Home Activity .kt file:
class HomeActivity : AppCompatActivity() {
private lateinit var binding: HomeActivityBinding
private lateinit var navController: NavController
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = HomeActivityBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
// ...
// Set-up bottom navigation menu & Side menu
val navView: NavigationView = binding.navView
val bottomNavView: BottomNavigationView = findViewById(R.id.bottom_nav_view)
val drawerLayout: DrawerLayout = binding.homeLayout // from navi drawer structure
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment_home_activity) as NavHostFragment
navController = navHostFragment.navController
// val navController = findNavController(R.id.nav_host_fragment_home_activity)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.fragment_home,
R.id.fragment_2,
R.id.fragment_3,
R.id.fragment_4,
R.id.fragment_5
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
bottomNavView.setupWithNavController(navController)
}
If you would like me to attach more Kotlin files let me know which ones.
Solution
Eventually solved my own problem. I moved all the nested destinations into the top level nav graph.
Answered By - ryanflynn001
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.