udacity/andfun-kotlin-sleep-tracker-with-recyclerview

App crashes on navigation after orientation change

amanaggarwal1 opened this issue · 2 comments

When user tries to navigate after rotating the phone, app crashes.
The following error is shown in the logcat:

com.example.android.trackmysleepqualityrecyclerview E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.trackmysleepqualityrecyclerview, PID: 11450
    java.lang.IllegalStateException: no current navigation node
        at androidx.navigation.NavController.navigate(NavController.java:765)
        at androidx.navigation.NavController.navigate(NavController.java:745)
        at androidx.navigation.NavController.navigate(NavController.java:731)
        at androidx.navigation.NavController.navigate(NavController.java:863)
        at com.example.android.trackmysleepquality.sleeptracker.SleepTrackerFragment$onCreateView$2.onChanged(SleepTrackerFragment.kt:93)
        at com.example.android.trackmysleepquality.sleeptracker.SleepTrackerFragment$onCreateView$2.onChanged(SleepTrackerFragment.kt:39)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
        at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
        at com.example.android.trackmysleepquality.sleeptracker.SleepTrackerViewModel$onStopTracking$1.invokeSuspend(SleepTrackerViewModel.kt:224)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

This crash can also be fixed by using the current version of the navigation dependencies [1, 2] as described here.

In the project gradle:

  1. set version_navigation = "2.3.5"
  2. replace classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:$version_navigation" with classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$version_navigation".

In the app gradle use:

// Navigation
implementation "androidx.navigation:navigation-fragment-ktx:$version_navigation"
implementation "androidx.navigation:navigation-ui-ktx:$version_navigation"

Fixed in this recent commit.