Skip to content

Instantly share code, notes, and snippets.

@rajendhirandev
Created October 2, 2022 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajendhirandev/4734f8ac5a532838cbb7f842f2f435ee to your computer and use it in GitHub Desktop.
Save rajendhirandev/4734f8ac5a532838cbb7f842f2f435ee to your computer and use it in GitHub Desktop.
NavController Usage Activity and Fragment Basics -- Ref. Code Snippet
// Code Snippet
// Activity ActionBar with NavController
lateinit var binding: ActivityHomeBinding
lateinit var navController: NavController
lateinit var appBarConfig: AppBarConfiguration
private fun initialize() {
setSupportActionBar(binding.toolbar)
// While Using FragmentContainerView
navController =
(supportFragmentManager.findFragmentById(R.id.navigation_fragment_view) as NavHostFragment).navController
// While Using Fragment
//navController = findNavController(R.id.navigation_fragment_view)
// Add Appbar config and navigation controller to the ActionBar
appBarConfig = AppBarConfiguration(navController.graph)
setupActionBarWithNavController(navController, appBarConfig)
}
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp(appBarConfig) || super.onSupportNavigateUp()
}
// In Fragment
//Param values through Bundle
findNavController().navigate(R.id.action_consumer_to_detail_fragment, bundleData)
// Safe-args as Primitive values
val action = ConsumerFragmentDirections.actionConsumerToDetailFragment(consumerName, consumerYoB)
findNavController().navigate(action)
//Parcelize Data Object
val action = ConsumerFragmentDirections.actionConsumerToDetailFragment(ConsumerData(consumerName, consumerYoB))
findNavController().navigate(action)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment