Describe the problem
Click the back/up button the application quits/exits stops.
Steps to reproduce?
Go to... any fragment other than the start fragment
Click on... the back arrow or up arrow in the action bar.
See error...
kotlin.UninitializedPropertyAccessException: lateinit property navController has not been initialized
at com.example.cupcake.MainActivity.onSupportNavigateUp(MainActivity.kt:52)
VersionsAndroid Studio version: 2020.3.1 Patch 2
API version of the emulator: 30.9.5-7820599
Additional informationInclude screenshots if they would be useful in clarifying the problem.
package com.example.cupcake
Activity for cupcake order flow.
*/
class MainActivity : AppCompatActivity(R.layout.activity_main) {
// Make navController a class variable so you can use it in another method.
private lateinit var navController: NavController
// override the onCreate() method to set up the navigation controller. Get an instance of
// NavController from the NavHostFragment.
// Make a call to setupActionBarWithNavController(navController) passing in the instance of
// NavController. This will do the following: Show a title in the app bar based off of the
// destination's label, and display the Up button whenever you're not on a top-level
// destination.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
setupActionBarWithNavController(navController)
}
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp() || super.onSupportNavigateUp()
}
URL of codelab
https://developer.android.com/codelabs/basic-android-kotlin-training-navigation-backstack?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-kotlin-unit-3-pathway-4%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-training-navigation-backstack#2 In which task and step of the codelab can this issue be found?
Describe the problem Click the back/up button the application quits/exits stops.
Steps to reproduce?
Additional information Include screenshots if they would be useful in clarifying the problem. package com.example.cupcake
import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.navigation.NavController import androidx.navigation.fragment.NavHostFragment import androidx.navigation.ui.setupActionBarWithNavController
/**
Activity for cupcake order flow. */ class MainActivity : AppCompatActivity(R.layout.activity_main) {
// Make navController a class variable so you can use it in another method.
private lateinit var navController: NavController
// override the onCreate() method to set up the navigation controller. Get an instance of // NavController from the NavHostFragment. // Make a call to setupActionBarWithNavController(navController) passing in the instance of // NavController. This will do the following: Show a title in the app bar based off of the // destination's label, and display the Up button whenever you're not on a top-level // destination.
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)
}
override fun onSupportNavigateUp(): Boolean { return navController.navigateUp() || super.onSupportNavigateUp() }
}