Day80 of #100DaysOfCode

Day80 of #100DaysOfCode

Hii folks 🙌

Today I will be continuing the same pathway in which we will implement ‘Up button’ behavior.

Unit 3: Navigation

Pathway 3: Advanced navigation app Examples

Implement Up button behavior

  • In the Cupcake app, the app bar shows an arrow to return to the previous screen. This is known as the Up button, and we’ve learned it in previous code labs. The Up button currently doesn’t do anything, so we will fix this navigation bug in the app.
  • In the MainActivity, we will make navController a class variable so we can use it in another method.
class MainActivity : AppCompatActivity(R.layout.activity_main) {    private lateinit var navController: NavController    override fun onCreate(savedInstanceState: Bundle?) {
val navHostFragment = supportFragmentManager
.findFragmentById( as NavHostFragment
navController = navHostFragment.navController
  • Within the same class, we will add code to override the onSupportNavigateUp() function. This code will ask the navController to handle navigating up in the app. Otherwise, fall back to back to the superclass implementation (in AppCompatActivity) of handling the Up button.
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp() || super.onSupportNavigateUp()
  • Now we will run the app. The Up button should now work from the FlavorFragment, PickupFragment, and SummaryFragment. As we navigate to previous steps in the order flow, the fragments should show the right flavor and pick-up date from the view model.

That is all for Day80 ✅

Thanks for reading, See you tomorrow!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store