Skip to content

Instantly share code, notes, and snippets.

@ahinchman1
Last active May 11, 2020 04:42
Show Gist options
  • Save ahinchman1/f3ebda7af250de79f21109a7924964b9 to your computer and use it in GitHub Desktop.
Save ahinchman1/f3ebda7af250de79f21109a7924964b9 to your computer and use it in GitHub Desktop.
class MainActivity : SingleFragmentActivity() {
...
private val navController by lazy {
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.fragment_container) as NavHostFragment
navHostFragment.navController
}
val navigator by lazy { Navigator(navController, resources) }
private val appBarConfigurations by lazy {
AppBarConfiguration
.Builder(topLevelDestinations)
.setDrawerLayout(drawerLayout)
.build()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setupNavigation()
}
/**
* Navigation Component setup
*/
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(navController, appBarConfigurations)
}
override fun onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
private fun setupNavigation() {
// Update action bar to reflect navigation
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfigurations)
// Handle nav drawer item clicks
navigationView.setNavigationItemSelectedListener { menuItem ->
menuItem.isChecked = true
drawerLayout.closeDrawers()
true
}
// Tie nav graph to items in nav drawer
NavigationUI.setupWithNavController(navigationView, navController)
}
companion object {
// The Up button will not be displayed at top-level destinations in the information hierarchy
val topLevelDestinations = hashSetOf(
R.id.errorFragment,
R.id.invasiveSpeciesMapFragment,
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment