Skip to content

Instantly share code, notes, and snippets.

@Akhu
Created November 7, 2020 13:06
Show Gist options
  • Save Akhu/f1b56f45af5f24596304178d16731aa1 to your computer and use it in GitHub Desktop.
Save Akhu/f1b56f45af5f24596304178d16731aa1 to your computer and use it in GitHub Desktop.
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/**
* We assign the navController from the XML interface.
* The Interface code contains everything needed
* to link the fragment to the navGraph and to set it as default navHost
*/
navController = findNavController(R.id.host_fragment)
/**
* We can call this function to make the navbar update accordingly to the navController
* Android + Jetpack handle this for us
*/
setupActionBarWithNavController(navController)
}
/**
* This is called when the back button of the action bar is clicked
*/
override fun onSupportNavigateUp(): Boolean {
/**
* We delegate the behavior of the "back" button from the action Bar
* to the navigation controller
*/
return navController.navigateUp()
}
override fun onBackPressed() {
navController.navigateUp()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment