set default destination in navigation architecture component based on user type

Viewed 328

I'm working on navigation component architecture and I face problem in how to set start destination after I authenticate user since I have two type of users :admin and user and I made my app contains 4 activities loginActivity ,signUpActivity userMainActivity : that use navigation component and contain all fragments related to user and adminMainActivity :that use navigation component and contain all fragments related to admin

I'm not able to figure out how to handle this and start user or admin activity after login by set default
should I put user and admin in separate navigation graph or what to in this case

1 Answers

you can set start destination programmatically, just like:

val navHostFragment = nav_host_fragment as NavHostFragment
val graphInflater = navHostFragment.navController.navInflater
navGraph = graphInflater.inflate(R.navigation.nav_graph)
navController = navHostFragment.navController

val destination = if (intent.getBooleanExtra(IS_ADMIN,false))
R.id.adminhomeFragment 
  else R.id.userhomeFragment 
navGraph.startDestination = destination
navController.graph = navGraph
Related