google-developer-training / android-basics-kotlin-cupcake-app

Apache License 2.0
103 stars 163 forks source link

Android Basics: Navigation backstack #25

Closed jetsystemservices closed 3 years ago

jetsystemservices commented 3 years ago

URL of codelab https://developer.android.com/codelabs/basic-android-kotlin-training-navigation-backstack?authuser=4&continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-kotlin-unit-3-pathway-4%3Fauthuser%3D4%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? Step 3.2

Describe the problem override fun onSupportNavigateUp(): Boolean { return navController.navigateUp() || super.onSupportNavigateUp() }` navController unresolved reference

Steps to reproduce? 1.Load: https://github.com/google-developer-training/android-basics-kotlin-cupcake-app/tree/viewmodel 2.Import to Android Studio 3.Write the code according to step 3.2

Versions Android Studio version: 4.2.2 API version of the emulator: 19

Additional information Include screenshots if they would be useful in clarifying the problem. image

Gaebobman commented 3 years ago

It's scope problem of val navController You have to Declare navController outside fun onCreate() but inside of MainActivity class Try


class MainActivity : AppCompatActivity(R.layout.activity_main){

    private lateinit var navController: NavController

    ......
    ......
    ......
}

Sorry for my broken English, English is not my first language Hehe

kkuan2011 commented 3 years ago

Yup, @Gaebobman is correct, declare navController as a class variable and then you can access it within the onSupportNavigateUp() method.

Solution code: https://github.com/google-developer-training/android-basics-kotlin-cupcake-app/blob/main/app/src/main/java/com/example/cupcake/MainActivity.kt